iOS Check for version updates

Source: Internet
Author: User

The original check code for the project applies to the 1.2 1.3 format. The general normal project format should be 1.2.2, so large version. Small version format.

Paste the code

-(void) oncheckversion{dispatch_queue_t queue = dispatch_get_global_queue (Dispatch_queue_priority_default, 0);        Dispatch_async (queue, ^{nsdictionary *infodic = [[NSBundle mainbundle] infodictionary];        NSString *currentversion = [infodic objectforkey:@ "cfbundleshortversionstring"];        NSString *url = @ "Http://itunes.apple.com/lookup?id=xxx";        Nsmutableurlrequest *request = [[Nsmutableurlrequest alloc] init];        [Request Seturl:[nsurl Urlwithstring:url];        [Request sethttpmethod:@ "POST"];        Nshttpurlresponse *urlresponse = nil;        Nserror *error = nil; NSData *recerveddata = [nsurlconnection sendsynchronousrequest:request returningresponse:&urlresponse Error:        &error]; NSString *results = [[nsstring alloc] initwithbytes:[recerveddata bytes] length:[recerveddata length] Encoding:        Nsutf8stringencoding]; Nsdictionary *dic = [nsjsonserialization jsonobjectwithdata:[results datausingencoding:nsutf8stringencoding] OptioNs:kniloptions Error:nil];        Nsarray *infoarray = [dic objectforkey:@ "Results"];            if ([Infoarray count]) {nsdictionary *releaseinfo = [Infoarray objectatindex:0];            NSString *lastversion = [releaseinfo objectforkey:@ "version"];  if ([self versionnumberconversion:lastversion] >[self versionnumberconversion:currentversion]) {alert = [Uialertcontroller alertcontrollerwithtitle:@ "discover new version" Message:[releaseinfo objectforkey:@ "ReleaseNotes"]                Preferredstyle:uialertcontrollerstylealert];                Uialertaction *cancelaction = [uialertaction actionwithtitle:@ "temporarily not" style:uialertactionstylecancel Handler:nil];                [Alert addaction:cancelaction]; Uialertaction *sureaction = [uialertaction actionwithtitle:@ "Update" Style:uialertactionstyledefault handler:^ ( Uialertaction * _nonnull action) {//Nsurl *url = [Nsurl urlwithstring:@] Https://itunes.apple.com/cn/ap      P/xxname/idxxx?l=en&mt=8 "];              Nsurl *url = [Nsurl urlwithstring:@ "itms-apps://itunes.apple.com/cn/app/xxname/idxxx?mt=8"];                [[UIApplication Sharedapplication]openurl:url];                }];                [Alert addaction:sureaction]; Dispatch_async (Dispatch_get_main_queue (), ^{[self presentviewcontroller:alert animated:yes completion                : nil];            }); }        }    });}
-(int) Versionnumberconversion: (nsstring*) version{    //If the version number 1.5.1 the following side need to determine several decimals, to determine the number of arrays.    int num1 = 0;    Nsarray *ary = [version componentsseparatedbystring:@ "];    if (Ary.count) {for        (int i = 0; i<ary.count; i++) {            if (i==0) {                NUM1 = [ary[0] intvalue]*10000;            } else if (i==1) {                NUM1 = [ary[0] intvalue]*10000+[ary[1] intvalue]*100;            } else if (i==2) {                NUM1 = [ary[0] intvalue]*10000+[ary[1] intvalue]*100+[ary[2] intvalue];            } else if (i>=3) {                NUM1 = [ary[0] intvalue]*10000+[ary[1] intvalue]*100+[ary[2] intvalue];}}    //nslog (@ "version number%d", NUM1);    return NUM1;}

  

  

iOS Check for version updates

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.