Krupa Parsaniya
Krupa Parsaniya

Reputation: 287

How to set different Google-Service-info-plist file according to environment in iOS Swift?

I want to set different google-service-plist file for different environment in same project for firebase integration

Upvotes: 1

Views: 790

Answers (3)

ManWithBear
ManWithBear

Reputation: 2875

Swift version of tom-baranowicz answer

#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
}

Upvotes: 0

Krupa Parsaniya
Krupa Parsaniya

Reputation: 287

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 added one key in info-plist file enter image description here

Upvotes: 2

Tom Baranowicz
Tom Baranowicz

Reputation: 111

This is objc solution, but it can be easily translated into Swift, i hope it will help:

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

Upvotes: 1

Related Questions