iOS -- 版本更新检查

   NetworkTool.checkUpdate {
    
     releaseNotes,isNeedUpdate in
        print(releaseNotes,isNeedUpdate)
        if isNeedUpdate {
    
    
            //弹框提示更新
            if !kWindow.subviews.contains(self.updateView) {
    
    
                kWindow.addSubview(self.updateView)
            }
        }  else {
    
    
            //不需要更新
        }
    }

工具类中的代码 NetworkTool

 //MARK: 版本更新检查
    static func checkUpdate(_ handler:((_ releaseNotes:String,_ isNeedUpdate:Bool)->())?) {
    
    
        let localVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
         NetworkTool.checkVersion {
    
     (appStoreVersion,releaseNotes) in
            var boolValue = false
            print("localVersion=",localVersion,"appStoreVersion=",appStoreVersion)
            if appStoreVersion == "0.0" {
    
    
                boolValue = false
            }
            let result = localVersion.compare(appStoreVersion)
            if result == .orderedAscending {
    
    //appStoreVersion大,需要弹出强制更新
                boolValue = true
            }
            else {
    
    
                boolValue = false
            }
            handler?(releaseNotes,boolValue)
        }
    }
    //appStoreVersionUrl:https://itunes.apple.com/cn/lookup?id=xxxxxxxxxxxx
    static func checkVersion(success:@escaping((String,String)->())) {
    
    
        let appstore_url = URL(string:appStoreVersionUrl)
        if let url = appstore_url {
    
    
            AF.request(url).responseJSON {
    
     (response) in
                if let value = response.value {
    
    
                    let dic = value as! Dictionary<String, Any>
                    print(dic)
                    let results = dic["results"] as! [[String:Any]]
                    if results.count >= 1 {
    
    
                        let dict = results.first!
                        let version = "\(dict["version"] ?? 0.0)"
                        let releaseNotes = "\(dict["releaseNotes"] ?? "")" //本次更新内容
                        success(version,releaseNotes)
                    }
                }
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/weixin_43259805/article/details/123260307