swift3 去appStore检查更新
2017-10-20 本文已影响0人
消防员杨
//去appStore检查更新
private func checkUpdate() {
let path = NSString(format: "http://itunes.apple.com/cn/lookup?id=%@", "自己的AppID") as String
let url = NSURL(string: path)
let request = NSMutableURLRequest(url: url! as URL, cachePolicy: NSURLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 10)
request.httpMethod = "POST"
NSURLConnection.sendAsynchronousRequest(request as URLRequest, queue: OperationQueue()) { (response, data, error) in
let receiveStatusDic = NSMutableDictionary()
if data != nil {
do {
let dic = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String: Any]
print(dic)
if let resultCount = dic["resultCount"] as? NSNumber {
if resultCount.intValue > 0 {
receiveStatusDic.setValue("1", forKey: "status")
if let arr = dic["results"] as? NSArray {
if let dict = arr.firstObject as? NSDictionary {
if let version = dict["version"] as? String {
receiveStatusDic.setValue(version, forKey: "version")
UserDefaults.standard.set(version, forKey: "Version")
UserDefaults.standard.synchronize()
}
}
}
}
}
}catch let error {
receiveStatusDic.setValue("0", forKey: "status")
}
}else {
receiveStatusDic.setValue("0", forKey: "status")
}
self.performSelector(onMainThread: #selector(self.checkUpdateWithData(data:)), with: receiveStatusDic, waitUntilDone: false)
}
}
@objc private func checkUpdateWithData(data: NSDictionary) {
let status = data["status"] as? String
let localVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
if status == "1" {
let storeVersion = data["version"] as! String
self.compareVersion(localVersion: localVersion, storeVersion: storeVersion)
return
}
if let storeVersion = UserDefaults.standard.object(forKey: "Version") as? String {
self.compareVersion(localVersion: localVersion, storeVersion: storeVersion)
}
}
private func compareVersion(localVersion: String, storeVersion: String) {
if localVersion.compare(storeVersion) == ComparisonResult.orderedAscending {
print("去更新")
//let alertView = UIAlertView.init(title: "提示", message: "版本有更新,请前往AppStore下载", delegate: self, cancelButtonTitle: "取消", otherButtonTitles: "前往下载")
//alertView.tag = 1
//alertView.show()
} else {
print("是最新版本")
//SVPManager.svpShowMessage(message: "已经是最新版本了", timeInterval: nil)
}
}