首页 > 解决方案 > Swift/iOS - Firebase 远程配置在 App Delegate 中获取值

问题描述

import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GADInterstitialDelegate {
    
    //START OF CONFIGURATION

    static let BANNER_ADS_ON = true

如何在 Firebase 远程配置中更改 BANNER_ADS_ON

标签: iosswiftfirebase-remote-config

解决方案


使用Firebase 远程配置应该不复杂。请阅读 Firebase 的文档和教程:https ://firebase.google.com/docs/remote-config/use-config-ios#swift


为了给你更多的想法,在你的AppDelegate,你会做这样的事情:

private func setupRemoteConfig() {
    // Interval 1 day if prod. Otherwise 0.
    let interval: TimeInterval = ENVManager.currentEnv == .production ? (60 * 60 * 24 * 1) : 0
    
    let settings = RemoteConfigSettings()
    settings.minimumFetchInterval = interval
    
    let remoteConfig = RemoteConfig.remoteConfig()
    remoteConfig.configSettings = settings
    
    remoteConfig.fetch(withExpirationDuration: interval) { (status, error) in
        if status == .success {
            print("Config fetched!")
            remoteConfig.activate() { (error) in
                // ...
            }
        } else {
            print("Config not fetched")
            print("Error: \(error?.localizedDescription ?? "No error available.")")
        }
    }
}

然后要访问您的值,在您的情况下,它是BANNER_ADS_ON或您想要的任何键,如下所示:

let configData = RemoteConfig.remoteConfig()["BANNER_ADS_ON"].stringValue

推荐阅读