iOS检查更新
2017-10-22 本文已影响0人
lalala1112389
在AppDelegate的DidFinish方法中调用
self.checkUpdatePlist()
func checkUpdatePlist() {
HWUtil.shared.checkUpdates({ (appVersion, ipaVersion, ipaDesc, ipaDownloadPath) in
let desc = "当前版本号:\(appVersion)\n 最新版本号:\(ipaVersion)\(ipaDesc)"
self.showUpdateAlert("软件有新版本可更新",desc, ipaDownloadPath)
}, nil, nil)
}
func checkUpdates(_ haveUpdate: ((_ appVersion: String, _ ipaVersion: String, _ ipaDescription: String, _ downloadPath: String)->())?, _ noUpdate: (() -> ())?, _ fail: ((URL?, URLResponse?, Error?) -> ())?) {
DispatchQueue.global().async {
// https://114.242.84.115/ota/ios/hwnews.plist
let url = URL.init(string: "https://114.242.84.115/ota/ios/hwnews.plist")
let request = URLRequest.init(url: url!, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 10)
let session = URLSession.init(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: nil)
let downlaodTask = session.downloadTask(with: request, completionHandler: { (url, response, error) in
if error == nil {
if let plistData = NSDictionary.init(contentsOf: url!) {
let ipaDownloadPath = "itms-services://?action=download-manifest&url=https://114.242.84.115/ota/ios/hwnews.plist"
debugPrint(plistData)
let items = plistData["items"] as! [Any]
let itemDict = items.first as! [String: Any]
let metadata = itemDict["metadata"] as! [String:String]
let ipaVersion = metadata["bundle-version"]
let description = metadata["description"]
let currentVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
if (self.compareVersions(v1: ipaVersion!, v2: currentVersion!)) {
// 更新
haveUpdate?(currentVersion!, ipaVersion!, "", ipaDownloadPath) // 暂时隐藏
// haveUpdate?(currentVersion!, ipaVersion!, "\n\(description!)", ipaDownloadPath)
} else {
noUpdate?()
}
} else {
noUpdate?()
}
} else {
fail?(url, response, error)
}
})
downlaodTask.resume()
}
}
//提示更新
func showUpdateAlert(_ title: String, _ verisondescription: String, _ urlString: String) {
let alert = UIAlertController.init(title: title, message: verisondescription, preferredStyle: .alert)
let cancelAction = UIAlertAction.init(title: "以后再说", style: .cancel, handler: { [weak self] (action) in
// cancel operation
if UserDefaults.standard.object(forKey: kUserLogined) as! Bool == true {
// 已登录
debugPrint("")
let rootVC = self?.window?.rootViewController
if (rootVC?.isKind(of: LoginViewController.self))! {
// present
let tabBarController = BaseTabBarController()
tabBarController.delegate = self
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.1, execute: {
rootVC?.present(tabBarController, animated: true, completion: nil)
})
}
}
})
let removeAction = UIAlertAction.init(title: "下载", style: .default, handler: { [weak self] (action) in
if UserDefaults.standard.object(forKey: kUserLogined) as! Bool == true {
// 已登录
debugPrint("")
let rootVC = self?.window?.rootViewController
let tabBarController = BaseTabBarController()
tabBarController.delegate = self
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.1, execute: {
rootVC?.present(tabBarController, animated: true, completion: nil)
})
}
// download operation
print("打开 sarafi")
let url = URL.init(string: urlString)
if #available(iOS 10.0, *) {
//高于 iOS 10.0
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
} else {
//低于 iOS 10.0
UIApplication.shared.openURL(url!)
}
})
alert.addAction(cancelAction)
alert.addAction(removeAction)
self.window?.rootViewController?.present(alert, animated: false, completion: nil)
}