简体   繁体   中英

How to show Firebase remote config in a ViewController

I'm trying to show remote config but I don't know what to do in the view

I have a model:

struct PromoPill: Codable {

let imageUrl: String?
let imageWidth: Int?
let imageHeight: Int?
let targetUrl: String?
let enabled: Bool?
let position: Int?

And a manager that has a function that collects the json data

extension PromoPillManager : PromoPillGetDataProtocol {


func getItemFromRemoteConfig() -> PromoPill? {
    var promopill: PromoPill? = nil
    let jsonString = RemoteConfig.remoteConfig().configValue(forKey: RemoteConfigKey.promo_pill.rawValue).stringValue!
    do {
        let decoder = JSONDecoder()
        let jsonData = jsonString.data(using: .utf8)!
        let decodedPill = try decoder.decode(PromoPill.self, from:jsonData)
        promopill = decodedPill
    } catch {
        print(error)
        return nil
    }
    return promopill
}

I have never used remote config and I don't know how to call this manager from the view, if someone can help, I would appreciate it

At the ViewController I have this function which parses the data

    private func getActualPromoPill() -> PromoPill {
    var promoPill = PromoPill(imageUrl: nil, imageWidth: nil, imageHeight: nil, targetUrl: nil, enabled: nil, position: nil)
    if let promoPillDone = PromoPillManager.shared.getItemFromRemoteConfig(){
        promoPill = promoPillDone
        PromoPillManager.shared.saveStore(object: promoPill)
    }else{
        if let stored = PromoPillManager.shared.readStored(){
            promoPill = stored
        }else{
            return promoPill
        }
    }
    return promoPill
}

you should register on the firebase console https://console.firebase.google.com/

after you have defined your app and... you should define your key and value to RemoteConfig such as below pic: 这张照片

you should configure your firebase to your project pod:

https://firebase.google.com/docs/ios/installation-methods?authuser=0#cocoapods

or swift package manager

https://firebase.google.com/docs/ios/installation-methods?authuser=0#swift-package-manager

after config and add the namespace you need. you should use

https://firebase.google.com/docs/remote-config/get-started?platform=ios

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM