美文网首页
iOS APP如何实现检测更新

iOS APP如何实现检测更新

作者: CMD独白 | 来源:发表于2016-10-26 13:38 被阅读87次

    第一种是直接从AppStore上获取APP的版本号,然后已安装的App版本号比较判断是否需要更新,另一种是从服务器上获取APP的版本号,然后与已安装的App版本号比较判断是否需要更新。
    第一种方法检测更新方法的优点是:检测版本号是实时同步的;缺点是:苹果网络不稳定,检测更新有点延时,部分App获取不到任何参数。第二种检测更新方法的优点是:检测更新速度快、检测稳定;缺点是:和App Store上的应用版本号不同步(App上架需要审核时间,不确定什么时候成功更新到App Store上)。

    那么,我先来说一下第一种方法吧,先从AppStore上获取版本号,代码如下:

    //获取版本号
    -(void)Postpath
    {
        NSString *appId = @"0123456789";//这是随便写的,你可以在查看APP信息里找到AppleId
    NSString *urlStr = [[NSString alloc] initWithFormat:@"http://itunes.apple.com/lookup?id=%@",appId];
        NSURL *url = [NSURL URLWithString: urlStr];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                               cachePolicy:NSURLRequestReloadIgnoringCacheData
                                                           timeoutInterval:10];
        
        [request setHTTPMethod:@"POST"];
        
        
        NSOperationQueue *queue = [NSOperationQueue new];
        
        [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response,NSData *data,NSError *error){
            NSMutableDictionary *receiveStatusDic=[[NSMutableDictionary alloc]init];
            if (data) {
                
                NSDictionary *receiveDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
                if ([[receiveDic valueForKey:@"resultCount"] intValue]>0) {
                    
                    [receiveStatusDic setValue:@"1" forKey:@"status"];
                    [receiveStatusDic setValue:[[[receiveDic valueForKey:@"results"] objectAtIndex:0] valueForKey:@"version"]   forKey:@"version"];
                }else{
                    
                    [receiveStatusDic setValue:@"-1" forKey:@"status"];
                }
            }else{
                [receiveStatusDic setValue:@"-1" forKey:@"status"];
            }
            
            [self performSelectorOnMainThread:@selector(receiveData:) withObject:receiveStatusDic waitUntilDone:NO];
        }];
        
    }
    

    在这个方法里可以输出获取的版本号:

    -(void)receiveData:(id)sender
    {
        NSLog(@"receiveData=%@",sender);
        [self compareVersion:sender[@"version"]];
       
    }
    
    

    在这个方法里比较新旧版本号的大小:

    - (BOOL)compareVersion:(NSString *)serverVersion{
        
        //获取应用当前版本号
        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
        NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
        
        /*
         typedef NS_ENUM(NSInteger, NSComparisonResult)
         {
         NSOrderedAscending = -1L,//升序
         NSOrderedSame,   //等于
         NSOrderedDescending  //降序
         };
         */
        
        //比较当前版本和新版本号的大小
        if ([appVersion compare:serverVersion options:NSNumericSearch] == NSOrderedAscending) {
            NSLog(@"发现新版本: %@",serverVersion);
            return YES;
        }else{
            NSLog(@"没有新版本");
            return NO;
        }
        
    }
    
    

    第二种方法,是从服务器上获取版本号进行判断的,代码如下:

    -(void)reloadData{
    
     NSString *BaseUrl = @"";
        NSDictionary *dic = [NSDictionary dictionary];
        dic = @{@"FunName":@"Get_App_Version",@"Params":@{@"DATA":@"2"}};
        HttpRequest *respont = [HttpRequest share];
        [respont sendRequestWithUrlNSString:BaseUrl withDic:dic withCompletion:^(NSDictionary *responseData, id status, NSError *error) {
            
            //判断版本
            NSLog(@"%@",responseData);
            if ([[responseData objectForKey:@"FAG"]isEqualToString:@"1"]) {
    
    //获取当前应用版本号
                NSDictionary *infoDict=[[NSBundle mainBundle] infoDictionary]; 
                NSString *version=[infoDict objectForKey:@"CFBundleShortVersionString"];
                //            NSLog(@"%@",version);
                NSDictionary *VersionDic = [responseData objectForKey:@"RET"];
    //获取到服务器上最后的版本号
                NSString *backVersion = [VersionDic objectForKey:@"IOS_VERSION"];
                
                if(backVersion==nil)
                {
                  return;
                }
    //VersionDic  objectForKey:@"IOS_Index"]判断是否在审核期间,如果在审核期间为1,没在审核期间为0,后台给的
                if([version isEqualToString:backVersion] == NO&&[[VersionDic  objectForKey:@"IOS_Index"] isEqualToString:@"0"])
                {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        
                        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"软件有新版本,是否下载" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
                        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                                NSLog(@"取消更新");          
                        }];
                        UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                            NSString *updateUrl = [VersionDic objectForKey:@"IOS_URL"];//后台给的
                            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:updateUrl]];
                        }];
                        [alertController addAction:cancelAction];
                        [alertController addAction:otherAction];
                        
                        [self presentViewController:alertController animated:YES completion:nil];
                        
                    });
                    
                    
                }else{
                    dispatch_async(dispatch_get_main_queue(), ^{
    
                    });
                }
            }
        }];
    
    }
    

    相关文章

      网友评论

          本文标题:iOS APP如何实现检测更新

          本文链接:https://www.haomeiwen.com/subject/odzmyttx.html