• iOS 检查更新


     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 view
    17     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                     }else
    51                     {
    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         }else
    60         {
    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)buttonIndex
    78 {
    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 }
  • 相关阅读:
    js转化 保留2位小数
    python练习:打印九九乘法表
    PyCharm常用快捷键及工具
    python关键字
    Python学习资源
    Jira项目导入,被导入项目与目的系统数据类型不一致导入不成功的解决方案
    压测的时候到底要不要加集合点?
    Java Vuser协议JDBC脚本编写(MySQL)
    eclipse工具使用
    oracle忘记sys,system密码的解决方法
  • 原文地址:https://www.cnblogs.com/qq449832375/p/4681716.html
Copyright © 2020-2023  润新知