【问题标题】:How to set different Google-Service-info-plist file according to environment in iOS Swift?如何在 iOS Swift 中根据环境设置不同的 Google-Service-info-plist 文件?
【发布时间】:2019-10-23 05:56:04
【问题描述】:

我想为同一项目中的不同环境设置不同的 google-service-plist 文件以进行 firebase 集成

【问题讨论】:

    标签: ios swift


    【解决方案1】:
    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"
            }
        }
    }
    

    我在 info-plist 文件中添加了一个键

    【讨论】:

      【解决方案2】:

      这是 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];
      

      【讨论】:

        【解决方案3】:

        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
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-03-10
          • 2019-01-10
          • 1970-01-01
          • 1970-01-01
          • 2012-11-01
          相关资源
          最近更新 更多