ios开发之应用程序检查更新功能的实现

来源:互联网 发布:电脑锣编程是什么 编辑:程序博客网 时间:2024/05/17 21:27
[objc] view plain copy
  1. 前几周,凡是包含了“检查更新”按钮的iOS应用程序均被拒绝上架了,探其原因,发现iOS8之后的系统都可以自动给应用程序升级。大概是苹果方面认为让软件开发商再做一个“检查更新”功能有点多余。不过站在程序员的角度上讲,“检查更新”功能被认为是用户使用次数最少,但偏偏pm们特别钟爱的一个功能。pm非要加,但苹果审核非要拒绝你的app,怎么办呢?其实,苹果现在还没有严苛到不能在程序中出现一丁点和检查更新功能的代码,经过我的大胆尝试,只要不把“检查更新”几个字显示出来就行。那。。。问题来了,没有这个按钮,该如何检查更新呢?傻呀~在app每启动一次,就去检查更新一次不就完了吗?哈哈哈哈,耍个小聪明,成功的将app上线了。  
[objc] view plain copy
  1. #pragma mark 检查更新  
  2. -(void)onCheckVersion:(UIButton *)sender  
  3. {  
  4.     _actV.hidden = NO;  
  5.     NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];  
  6.     //CFShow((__bridge CFTypeRef)(infoDic));  
  7.     NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];  
  8.       
  9.     NSString *URL = @"http://itunes.apple.com/lookup?id=518966501";  
  10.       
  11.     // 1. 准备url地址  
  12.     NSString *urlStr = URL;  
  13.     NSURL *url = [NSURL URLWithString:urlStr];  
  14.       
  15.       
  16.     // 2. 创建请求对象  
  17.     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];  
  18.     // 2.1 请求方式(默认方式为GET,可不写)  
  19.     [request setHTTPMethod:@"GET"];  
  20.       
  21.     [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {  
  22.         _actV.hidden = YES;  
  23.         NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];  
  24.         NSArray *infoArray = [dic objectForKey:@"results"];  
  25.         if ([infoArray count]) {  
  26.             NSDictionary *releaseInfo = [infoArray objectAtIndex:0];  
  27.             NSString *lastVersion = [releaseInfo objectForKey:@"version"];  
  28.               
  29.             // 有新版本  
  30.             if ([lastVersion compare:currentVersion] > 0) {  
  31.                 // 根据主版本号判断是否需要强制更新  
  32.                 NSMutableString *cur1 = [NSMutableString stringWithString:currentVersion];  
  33.                 NSMutableString *cur2 = [NSMutableString stringWithString:currentVersion];  
  34.                 NSRange range = NSMakeRange(14);  
  35.                 [cur1 deleteCharactersInRange:range];  
  36.                 NSInteger curFirst1 = [cur1 integerValue];  
  37.                 NSRange range3 = NSMakeRange(04);  
  38.                 [cur2 deleteCharactersInRange:range3];  
  39.                 NSInteger curLast = [cur2 intValue];  
  40.                   
  41.                   
  42.                 NSMutableString *last1 = [NSMutableString stringWithString:lastVersion];  
  43.                 NSMutableString *last2 = [NSMutableString stringWithString:lastVersion];  
  44.                 [last1 deleteCharactersInRange:range];  
  45.                 NSInteger lastFirst1 = [last1 integerValue];  
  46.                 [last2 deleteCharactersInRange:range3];  
  47.                 NSInteger lastLast = [last2 integerValue];  
  48.                   
  49.                   
  50.                 // 主版本号更新大于1,强制更新  
  51.                 if (lastFirst1 - curFirst1 >=1 || (curFirst1 == lastFirst1 && curLast != lastLast)) {  
  52.                     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"版本更新" message:@"发现新版本,强烈建议您更新到最新版本,否则会影响您的正常使用" delegate:self cancelButtonTitle:@"更新" otherButtonTitles:nil, nil nil];  
  53.                     alert.tag = 100001;  
  54.                     [alert show];  
  55.                 }  
  56.                 // 祝版本号没有更新,不强制更新  
  57.                 else {  
  58.                     //trackViewURL = [releaseInfo objectForKey:@"trackVireUrl"];  
  59.                     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"有新的版本更新,是否前往更新?" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"更新", nil nil];  
  60.                     alert.tag = 100002;  
  61.                     [alert show];  
  62.                 }  
  63.             }  
  64.               
  65.             // 没有新版本  
  66.             else  
  67.             {  
  68.                 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"此版本为最新版本" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil];  
  69.                 alert.tag = 100003;  
  70.                 [alert show];  
  71.             }  
  72.         }  
  73.           
  74.     }];  
  75. }  
  76.   
  77. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
  78. {  
  79.     // 强制更新  
  80.     if (alertView.tag == 100001) {  
  81.         if (buttonIndex == 0) {  
  82.             [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunes.apple.com/cn/app/id518966501"]];  
  83.         }  
  84.           
  85.     }  
  86.     // 非强制更新  
  87.     else if (alertView.tag == 100002) {  
  88.         if (buttonIndex == 0) {  
  89.               
  90.         } else if (buttonIndex == 1) {  
  91.             [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunes.apple.com/cn/app/lan-bi-tou/id935232659?mt=8"]];  
  92.         }  
  93.           
  94.     }  
  95.     // 不需要更新  
  96.     else if (alertView.tag == 100003) {  
  97.           
  98.     }  
  99.   
  100. }  


原创粉丝点击