【问题标题】:How can I get array json value from Firebase remote-config ?(Swift)如何从 Firebase 远程配置获取数组 json 值?(Swift)
【发布时间】:2019-10-18 02:30:56
【问题描述】:

我使用 Firebase 远程配置来比较要更新的应用版本,但我无法获得远程配置值。

如何使用 swift 获取数组 json 之类的值?

这是我的价值

[
{
"version": "1.0.4",
"status": "2",
"text": "Fix Error"
}
]

这是我的代码

获取价值:

class ConstraintValues {
static let shared = ConstraintValues()
init() {
    RemoteConfig.remoteConfig().setDefaults(["a":"b" as NSObject])
    let debugSettings = RemoteConfigSettings()
    RemoteConfig.remoteConfig().configSettings = debugSettings
}
func fetch(completetion:@escaping ( _ stringarray:String?)->()) {
    RemoteConfig.remoteConfig().fetch(withExpirationDuration: 0) { (status, error) in
        if let error = error {
            print(error)
            completetion(nil)
            return
        }
        RemoteConfig.remoteConfig().activate()
        let rc = RemoteConfig.remoteConfig().configValue(forKey: "VERSION")

        do {
            let json = try JSONSerialization.jsonObject(with: rc.dataValue, options: .allowFragments) as! [String:String]

            guard let dictionary1 = json["version"] else {return }
            guard let dictionary2 = json["text"] else {return }
            guard let dictionary3 = json["status"] else {return }
            completetion(dictionary1)
            completetion(dictionary2)
            completetion(dictionary3)
        }
        catch let error{
            print(error)
            completetion(nil)
        }

    }
}}

检查版本:

func checkversion(controller:UIViewController){
    ConstraintValues.shared.fetch { (version) in
        let cversion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! NSString//localversion
         let url = URL(string: "https://www.google.com/")
        let appStore = version//newversion
        //Compare
        let versionCompare = cversion.compare(appStore! as String, options: .numeric)
        if versionCompare == .orderedSame {
            print("same version")
        } else if versionCompare == .orderedAscending {

            let alertc = UIAlertController(title: "UPDATE!!", message: "New Version: \(version!)", preferredStyle: .alert)
            let alertaction = UIAlertAction(title: "Update", style: .default, handler: { (ok) in
                if #available(iOS 10.0, *) {
                    UIApplication.shared.open(url!, options: [:], completionHandler: nil)
                } else {
                    UIApplication.shared.openURL(url!)
                }
            })
            alertc.addAction(alertaction)
            controller.present(alertc, animated: true, completion: nil)

        } else if versionCompare == .orderedDescending {

        }
    }
}

我可以获取“版本”:“1.0.4”,但“文本”无法获取。 这种方法应该怎么做?

【问题讨论】:

    标签: ios swift firebase firebase-remote-config


    【解决方案1】:

    由于您使用的是 JSON 对象,我建议您在此处使用 SwiftyJSON。

    在你的项目中安装SwiftyJSON

    然后在文件顶部导入SwiftyJSON

    您的代码应如下所示

    import Firebase
    import SwiftyJSON
    
    class MyViewController: UIViewController {
            
        var remoteConfig: RemoteConfig!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            remoteConfig = RemoteConfig.remoteConfig()
            let settings = RemoteConfigSettings()
            settings.minimumFetchInterval = 0
            remoteConfig.configSettings = settings
            
            var remoteConfigDefaults = [String: NSObject]()        
            let testArr: NSArray = []
            remoteConfigDefaults["VERSION"] = testStr
            remoteConfig.setDefaults(remoteConfigDefaults)        
            
            remoteConfig.fetch() { (status, error) -> Void in
                if status == .success {
                    print("Config fetched!")
                    self.remoteConfig.activate() { (error) in
                        // ...
                    }
                } else {
                    print("Config not fetched")
                    print("Error: \(error?.localizedDescription ?? "No error available.")")
                }
              
                let version = self.remoteConfig.configValue(forKey: "VERSION").jsonValue            
                let json = JSON(version)            
                print(json[0]["version"], json[0]["status"], json[0]["text"])
                
            }
        }
            
    }
    

    【讨论】:

      【解决方案2】:

      此解决方案在不使用 SwiftyJSON

      的情况下对我有用
      func getSomeValue() -> SomeValue? {
          let configValue = remoteConfig?.configValue(forKey: "some_key")
          if let data = configValue?.dataValue {       
              do {
                  let result = try JSONDecoder().decode(SomeValue.self, from: data)
                  return result
              } catch {
                  return nil
              }
          }
          return nil
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多