swift 获取Appstore版本号并与本地版本比较 更新跳转到Appstore

首先我用的是Alamofire请求的数据

1.获取appstore版本号

func checkVersionTapGestrue(sender: UITapGestureRecognizer){
        Alamofire.request("https://itunes.apple.com/lookup?bundleId=你的bundleId", method: .get, parameters: [:])
            .responseJSON { (response) in
                 
                if let value = response.result.value {
                    let json = JSON(value)
             
                    self.compareVersion(appStoreVersion: json["results"][0]["version"].stringValue)
                }
        }
    }

2.比较两个版本

//比较版本
    func compareVersion(appStoreVersion: String) {
     
        let version:String = (Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String)!
 
        if appStoreVersion == version {
            BaseController().showTextOnly(view: self.view, text: "当前已经是最新版本", detail: "")
        }else{
            let urlString = "itms-apps://itunes.apple.com/app/id23223232323"(id加你的appId)
            if let url = URL(string: urlString) {
                //根据iOS系统版本,分别处理
                if #available(iOS 10, *) {
                    UIApplication.shared.open(url, options: [:],
                                              completionHandler: {
                                                (success) in
                    })
                } else {
                    UIApplication.shared.openURL(url)
                }
            }
        }
    }   

参考:http://www.hangge.com/blog/cache/detail_1265.html

你可能感兴趣的:(swift 获取Appstore版本号并与本地版本比较 更新跳转到Appstore)