将 Firebase 添加至您的 iOS 项目
生成并添加配置文件
- 在 Firebase 控制台里创建项目,并填入 iOS 项目的 Bundle Identifier
- 下载 GoogleService-Info.plist 文件,在 Xcode 中以 Create groups 的方式添加该文件。
同步库文件
- 在
xxx.xcodeproj文件所在的目录下打开终端,使用pod init命令生成Podfile文件。 - 打开
Podfile,写入如下内容:platform :ios, '10.0' target 'xxx-mobile' do pod 'Firebase/Analytics' pod 'Firebase/Auth' pod 'Firebase/Firestore' end - 使用
pod install --repo-update命令下载第三方库。
初始化Firebase
- 下载第三方库成功后,打开
xxx.xcworkspace文件。 - 在
AppController.mm文件里包含头文件#import <Firebase.h>。 - 在
didFinishLaunchingWithOptions函数中添加初始化的代码[FIRApp configure]。
运行报错
我遇到了这样一个错误Undefined symbol: _OBJC_CLASS_$_FIRApp
在执行 pod update 和 pod 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 update 和 pod install 就可以了。