React Native 的 iOS Bridging Header 设置
发布: (2025年12月17日 GMT+8 02:51)
2 min read
原文: Dev.to
Source: Dev.to
设置 Bridging Header
-
创建 Bridging Header 文件
- 在 Xcode 中右键点击你的项目文件夹。
- 选择 New File → Header File。
- 将文件命名为
YourAppName-Bridging-Header.h。 - 勾选所有 target(主应用、development、production),然后点击 Create。
-
配置 Bridging Header 路径
对每个 target:- 打开 Build Settings。
- 搜索 “Objective‑C Bridging Header”。
- 将路径设置为
YourAppName/YourAppName-Bridging-Header.h。
导入 Objective‑C 头文件
打开 YourAppName-Bridging-Header.h 并添加所需的导入,例如:
// React Native Config
#import "RNCConfig.h"
// Google Maps
#import
// Your custom modules
#import "CustomNativeModule.h"
示例 AppDelegate.swift
import UIKit
import Firebase // Make sure Firebase is imported if used
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Access environment variables via React Native Config
if let apiKey = RNCConfig.env(for: "API_KEY") {
GMSServices.provideAPIKey(apiKey)
}
// Initialize Firebase
FirebaseApp.configure()
return true
}
}
清理、构建与运行
- 清理:
Cmd + Shift + K - 构建:
Cmd + B - 运行:
Cmd + R
项目结构概览
YourAppName/
├── ios/
│ ├── YourAppName/
│ │ ├── AppDelegate.swift
│ │ ├── YourAppName-Bridging-Header.h ✅
│ │ └── Info.plist
│ └── Pods/
└── package.json