将 Firebase 添加至您的 iOS 项目

官网文档

生成并添加配置文件

  1. 在 Firebase 控制台里创建项目,并填入 iOS 项目的 Bundle Identifier
  2. 下载 GoogleService-Info.plist 文件,在 Xcode 中以 Create groups 的方式添加该文件。

同步库文件

  1. xxx.xcodeproj文件所在的目录下打开终端,使用 pod init 命令生成 Podfile 文件。
  2. 打开 Podfile ,写入如下内容:
    platform :ios, '10.0'
    target 'xxx-mobile' do
    pod 'Firebase/Analytics'
    pod 'Firebase/Auth'
    pod 'Firebase/Firestore'
    end
  3. 使用 pod install --repo-update 命令下载第三方库。

初始化Firebase

  1. 下载第三方库成功后,打开 xxx.xcworkspace 文件。
  2. AppController.mm 文件里包含头文件 #import <Firebase.h>
  3. didFinishLaunchingWithOptions 函数中添加初始化的代码 [FIRApp configure]

运行报错

我遇到了这样一个错误Undefined symbol: _OBJC_CLASS_$_FIRApp

在执行 pod updatepod install 的时候,需要留意下面的那些提示,比如:

[!] The `work_crush_bonbons-mobile [Release]` target overrides the `GCC_PREPROCESSOR_DEFINITIONS` build setting defined in `Pods/Target Support Files/Pods-work_crush_bonbons-mobile/Pods-work_crush_bonbons-mobile.release.xcconfig'. This can lead to problems with the CocoaPods installation
    - Use the `$(inherited)` flag, or
    - Remove the build settings from the target.

然后我就按照它的提示,去找到 GCC_PREPROCESSOR_DEFINITIONS 这个设置,然后加上 $(inherited) ,再次执行 pod updatepod install 就可以了。