React Native 的 iOS Bridging Header 设置

发布: (2025年12月17日 GMT+8 02:51)
2 min read
原文: Dev.to

Source: Dev.to

设置 Bridging Header

  1. 创建 Bridging Header 文件

    • 在 Xcode 中右键点击你的项目文件夹。
    • 选择 New FileHeader File
    • 将文件命名为 YourAppName-Bridging-Header.h
    • 勾选所有 target(主应用、development、production),然后点击 Create
  2. 配置 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
Back to Blog

相关文章

阅读更多 »

Swift 和 UIKit 中的自定义字体

介绍 在本教程中,我将演示如何在 Swift 与 UIKit 中使用自定义字体。我将使用 Montserrat 和 Hind 字体,它们可以从…下载。