使用swift4对xcode中的单个项目使用不同的GoogleService-Info.plist [英] Use different GoogleService-Info.plist for single project in xcode using swift4

查看:60
本文介绍了使用swift4对xcode中的单个项目使用不同的GoogleService-Info.plist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,但是有4个不同的环境(开发,标记,质量检查,生产).我从移动设备的设置中给出了它们(环境的Web服务URL)路径.现在,我想对所有这些不同的环境使用不同的GoogleService-info.plist.就像我从后端选择Dev时,该项目应仅使用Dev项目的GoogleService-Info.plist.这些GoogleService-Info.plists是在4个不同的帐户上创建的.项目应以编程方式采用GoogleService-info.plist的路径.我尝试了以下代码

I have a single project but with 4 different environments (Dev,Stagging,QA,Production). I have given their (environment's webservice url) paths from mobile's setting. Now I want to use different GoogleService-info.plist for all these different environments. Like when I select Dev from backend the project should take GoogleService-Info.plist of Dev project only. These GoogleService-Info.plists are created on 4 different accounts. Project should take the path of GoogleService-info.plist programmatically. I have tried the following code

1]通过引用

1] By taking reference from this url , I have created two folders Dev and QA (for now) and tried to given their paths by programmaically

#if DEV
    print("[FIREBASE] Development mode.")
    filePath = Bundle.main.path(forResource: "GoogleService-Info", 
ofType: "plist", inDirectory: "Dev")
    #elseif QA
    print("[FIREBASE] QA mode.")
    filePath = Bundle.main.path(forResource: "GoogleService-Info", 
ofType: "plist", inDirectory: "QA")
    #endif
    let options = FirebaseOptions.init(contentsOfFile: filePath)!
    FirebaseApp.configure(options: options)

但是会引发错误

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

 let options = FirebaseOptions.init(contentsOfFile: filePath)!

此行

2]其次,我通过GoogleService-Info-QA.plist更改了GoogleService-Info.plist的名称,并尝试以编程方式访问此文件

2] Second I changed name of GoogleService-Info.plist by GoogleService-Info-QA.plist and tried to access this file programmatically

private func configureFirebase() {
    guard   let plistPath = Bundle.main.path(forResource: 
"GoogleService-Info-QA", ofType: "plist"),
        let options =  FirebaseOptions(contentsOfFile: plistPath)
        else { return }
    FirebaseApp.configure(options: options)
}

但是会引发错误

Terminating app due to uncaught exception 'FIRAppNotConfigured', 
reason: 'Failed to get default Firebase Database instance. Must 
call `[FIRApp configure]` (`FirebaseApp.configure()` in Swift) 
before using Firebase Database.

推荐答案

我找到了解决方案.我通过各自的环境将GoogleService-Info.plists文件重命名.并在构建阶段->复制捆绑资源中添加了这些文件.然后根据所选环境添加以下代码

I got the solution. I renamed GoogleService-Info.plists files by its respective environments. And Added those files in Build Phases -> Copy Bundle Resources. Then added the following code as per the environment selected

 guard let plistPath = Bundle.main.path(forResource: "nameOfTheGoogleService-Info.plistFileAsPerTheEnvironment", ofType: "plist"),
                let options =  FirebaseOptions(contentsOfFile: plistPath)
                else { return }
            if FirebaseApp.app() == nil{
            FirebaseApp.configure(options: options)
            }

较小的变化是,当用户更改应用程序的环境时,他应该从后台删除该应用程序,然后再次打开它.然后,AppDeleagte会根据其选择的环境采用相应GoogleService-Info.plist的路径

Minor change is that, when user changes the app's environment, then he should remove app from background and open it again. Then the AppDeleagte takes the path of respective GoogleService-Info.plist as per its environment selected

这篇关于使用swift4对xcode中的单个项目使用不同的GoogleService-Info.plist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆