iOS檢查版本更新

來源:互聯網
上載者:User

標籤:

項目原來的檢查代碼適用於1.2 1.3格式。一般正常的項目格式應該是1.2.2 ,如此大版本.小版本格式。

貼下代碼

-(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:@"發現新版本" message:[releaseInfo objectForKey:@"releaseNotes"] preferredStyle:UIAlertControllerStyleAlert];                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"暫不" style:UIAlertActionStyleCancel handler:nil];                [alert addAction:cancelAction];                UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {//                    NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/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{    //假如版本號碼1.5.1 如下 這邊需要確定幾位小數,來確定數組個數。    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(@"版本號碼%d",num1);    return num1;}

  

  

iOS檢查版本更新

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.