【发布时间】:2019-10-23 05:56:04
【问题描述】:
我想为同一项目中的不同环境设置不同的 google-service-plist 文件以进行 firebase 集成
【问题讨论】:
我想为同一项目中的不同环境设置不同的 google-service-plist 文件以进行 firebase 集成
【问题讨论】:
struct Configuration {
static var environment: Environment = {
return Environment.init(rawValue: Bundle.main.object(forInfoDictionaryKey: "Configuration") as! String)!
}()
static var documentsDir: String {
guard let dir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first else { fatalError("There was something wrong finding the documents directory") }
return dir
}
}
enum Environment: String {
case local = "Local"
case production = "Production"
var googlePlistFileName: String {
switch self {
case .local:
return "GoogleServiceMobileLocal"
case .production:
return "GoogleServiceMobileProduction"
}
}
}
【讨论】:
这是 objc 解决方案,但可以很容易地翻译成 Swift,希望对您有所帮助:
NSString *filePath;
#ifdef DEBUG
filePath = [[NSBundle mainBundle] pathForResource:@"GoogleService-Staging" ofType:@"plist"];
#else
filePath = [[NSBundle mainBundle] pathForResource:@"GoogleService-Production" ofType:@"plist"];
#endif
FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:filePath];
[FIRApp configureWithOptions:options];
【讨论】:
tom-baranowicz answer 的 Swift 版本
#if DEBUG
let path = Bundle.main.path(forResource: "GoogleService-Staging", ofType: "plist")
#else
let path = Bundle.main.path(forResource: "GoogleService-Production", ofType: "plist")
#endif
if let options = path.flatMap({ FirebaseOptions(contentsOfFile: $0) }) {
FirebaseApp.configure(options: options)
} else {
// handle failed configuration here
}
【讨论】: