iOS check update
Posted on 26 Haziran 2018 in Programlama by admin
Following is a swift code to check if update available.
let plistUrl = "https://xyz.com/manifest.plist" let installationUrl = "itms-services://?action=download-manifest&url=https://xyz.com/manifest.plist"
func checkForUpdates() { DispatchQueue.main.async(execute: { if let url = URL(string: self.plistUrl), let updateDictionary = NSDictionary(contentsOf: url) { let items = updateDictionary["items"] let itemDict = (items as AnyObject).lastObject as! NSDictionary let metaData = itemDict["metadata"] as! NSDictionary let serverVersion = metaData["bundle-version"] as! String let localVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String let updateAvailable = serverVersion.compare(localVersion, options: NSString.CompareOptions.numeric) == .orderedDescending; if updateAvailable { self.showUpdateDialog(serverVersion: serverVersion) } } }) } func showUpdateDialog(serverVersion: String) { let alertController = UIAlertController(title: "New version of MSP available", message: "MSP \(serverVersion) has been released. Would you like to download it now?", preferredStyle: UIAlertControllerStyle.alert) alertController.addAction(UIAlertAction(title: "Not now", style: .cancel,handler: self.updateHandler)) alertController.addAction(UIAlertAction(title: "Update", style: .default, handler: self.updateHandler)) self.present(alertController, animated: true, completion: nil) } func updateHandler(alert: UIAlertAction){ switch alert.title { case "Update"?: UIApplication.shared.openURL(URL(string: self.installationUrl)!) exit(0) case "Not now"?: break default: break } }