iOS development One sentence Code detection app version of update-swift version

Source: Internet
Author: User

Create a new NSObject class and copy the following code into this class.

hkcheckversionmanager.swift//projectmanager////Created by Ishakan on 2017/12/29.//Copyright? 2017 Liuhuakun. All rights Reserved.//import Uikitclass Hkcheckversionmanager:nsobject {////+////-Parameter A Ppid:apple ID-the developer account corresponding app gets init (appid:string) {super.init ()//Get the latest version number on AppStore let AP PUrl = Url.init (string: "http://itunes.apple.com/lookup?id=" + appId) Let appmsg = try?         String.init (contentsof:appurl!, encoding:. UTF8) Let appmsgdict:nsdictionary = getdictfromstring (jstring:appmsg!) Let Appresultsarray:nsarray = (appmsgdict["Results"] as?        Nsarray)! Let appresultsdict:nsdictionary = Appresultsarray.lastobject as! Nsdictionary Let appstoreversion:string = appresultsdict["Version"] as!                String Let appstoreversion_float:float = Float (appstoreversion)! Gets the version number used by the current phone installation let localversion:string = bundle.main.infodictionary! ["CfbundleshortversionstriNg "] as!                String Let localversion_float:float = Float (localversion)!        Whether the user is set to no longer prompt let Userdefaults = userdefaults.standard let res = Userdefaults.bool (forkey: "No_alert_again")            Version number on AppStore is greater than local version number-description is updated if appstoreversion_float > Localversion_float &&!res {  Let ALERTC = Uialertcontroller.init (title: "Version updated", message: "Whether to go to update", Preferredstyle:. Alert) Let Yesaction =            Uialertaction.init (title: "Go to update", style:. Default, Handler: {(handler) in Self.updateapp (Appid:appid) }) Let Noaction = Uialertaction.init (title: "Next Time", Style:. Cancel, Handler:nil) let CancelA            ction = Uialertaction.init (title: "No longer prompted", style:. Default, Handler: {(handler) in Self.noalertagain () }) alertc.addaction (yesaction) alertc.addaction (noaction) alertc.addaction (CancelA ction) UIApplication.shared.keyWindow? RootviEwcontroller?.        Present (ALERTC, Animated:true, Completion:nil)}}//Go to update func Updateapp (appid:string) {        Let Updateurl:url = Url.init (string: "http://itunes.apple.com/app/id" + appId)!        UIApplication.shared.open (UpdateUrl, Options: [:], Completionhandler:nil)}//no longer prompted for Func Noalertagain () { Let Userdefaults = Userdefaults.standard Userdefaults.set (true, Forkey: "No_alert_again") userdefaults.s Ynchronize ()}//jsonstring to dictionary func getdictfromstring (jstring:string), nsdictionary {let Jsondat        A:data = Jstring.data (using:. UTF8)! Let dict = try? Jsonserialization.jsonobject (With:jsondata, Options:. mutablecontainers) If dict! = Nil {return dict a s! Nsdictionary} return Nsdictionary ()}}

Call a code in the class that you want to detect

        Version detection        _ = Hkcheckversionmanager.init (appId: "Your Apple ID")

  

iOS development One sentence Code detection app version of update-swift version

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.