iOS 檢查更新

來源:互聯網
上載者:User

標籤:

 1 #pragma mark - 檢查更新 2  3 - (void)checkUpdateWithAPPID:(NSString *)APPID 4 { 5     //擷取當前應用版本號碼 6     NSDictionary *appInfo = [[NSBundle mainBundle] infoDictionary]; 7     NSString *currentVersion = [appInfo objectForKey:@"CFBundleVersion"]; 8      9     NSString *updateUrlString = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",APPID];10     NSURL *updateUrl = [NSURL URLWithString:updateUrlString];11     versionRequest = [ASIFormDataRequest requestWithURL:updateUrl];12     [versionRequest setRequestMethod:@"GET"];13     [versionRequest setTimeOutSeconds:60];14     [versionRequest addRequestHeader:@"Content-Type" value:@"application/json"];15     16     //loading view17     CustomAlertView *checkingAlertView = [[CustomAlertView alloc] initWithFrame:NAVIGATION_FRAME style:CustomAlertViewStyleDefault noticeText:@"正在檢查更新..."];18     checkingAlertView.userInteractionEnabled = YES;19     [self.navigationController.view addSubview:checkingAlertView];20     [checkingAlertView release];21     22     [versionRequest setCompletionBlock:^{23         24         [checkingAlertView removeFromSuperview];25         26         NSError *error = nil;27         NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[versionRequest responseData] options:NSJSONReadingMutableContainers error:&error];28         if (!error) {29             if (dict != nil) {30                 //            DLog(@"dict %@",dict);31                 int resultCount = [[dict objectForKey:@"resultCount"] integerValue];32                 if (resultCount == 1) {33                     NSArray *resultArray = [dict objectForKey:@"results"];34                     //                DLog(@"version %@",[resultArray objectAtIndex:0]);35                     NSDictionary *resultDict = [resultArray objectAtIndex:0];36                     //                DLog(@"version is %@",[resultDict objectForKey:@"version"]);37                     NSString *newVersion = [resultDict objectForKey:@"version"];38                     39                     if ([newVersion doubleValue] > [currentVersion doubleValue]) {40                         NSString *msg = [NSString stringWithFormat:@"最新版本為%@,是否更新?",newVersion];41                         newVersionURlString = [[resultDict objectForKey:@"trackViewUrl"] copy];42                         DLog(@"newVersionUrl is %@",newVersionURlString);43                         //                    if ([newVersionURlString hasPrefix:@"https"]) {44                         //                         [newVersionURlString replaceCharactersInRange:NSMakeRange(0, 5) withString:@"itms-apps"];45                         //                    }46                         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"暫不" otherButtonTitles:@"立即更新", nil];47                         alertView.tag = 1000;48                         [alertView show];49                         [alertView release];50                     }else51                     {52                         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您使用的是最新版本!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"確定", nil];53                         alertView.tag = 1001;54                         [alertView show];55                         [alertView release];56                     }57                 }58             }59         }else60         {61             DLog("error is %@",[error debugDescription]);62         }63     }];64     65     [versionRequest setFailedBlock:^{66         [checkingAlertView removeFromSuperview];67         68         CustomAlertView *alertView = [[CustomAlertView alloc] initWithFrame:NAVIGATION_FRAME style:CustomAlertViewStyleWarning noticeText:@"操作失敗,請稍候再試!"];69         [self.navigationController.view addSubview:alertView];70         [alertView release];71         [alertView selfRemoveFromSuperviewAfterSeconds:1.0];72     }];73     74     [versionRequest startSynchronous];75 }76 77 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex78 {79     DLog(@"newVersionUrl  is %@",newVersionURlString);80     if (buttonIndex) {81         if (alertView.tag == 1000) {82             if(newVersionURlString)83             {84                 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:newVersionURlString]];85             }86         }87     }88 }

 

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.