首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据iOS Swift中的环境设置不同的文件?

如何根据iOS Swift中的环境设置不同的文件?
EN

Stack Overflow用户
提问于 2019-10-23 05:56:04
回答 3查看 388关注 0票数 1

我想在同一个项目中为不同的环境设置不同的google-service-plist文件,以便进行防火墙集成。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-10-23 08:21:47

代码语言:javascript
复制
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"
        }
    }
}

I在信息plist文件中添加了一个键

票数 2
EN

Stack Overflow用户

发布于 2019-10-23 06:29:33

这是objc解决方案,但是它可以很容易地转换成Swift,我希望它会有所帮助:

代码语言:javascript
复制
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];
票数 1
EN

Stack Overflow用户

发布于 2019-10-23 08:52:43

tom-baranowicz answer的快速版本

代码语言:javascript
复制
#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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58516306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档