iOS check update

Posted on 26 Haziran 2018 in Programlama by

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
        }
    }

Please give us your valuable comment

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image

This site uses Akismet to reduce spam. Learn how your comment data is processed.