iOS版本检测更新

/**

 *  @author KuYu_YX

 *

 每个正在迭代更新的项目,都必不可少的要检测是否发布了最新的版本,来保证以最优化的版本服务用户。其实我并不赞成一打开程序就去检测,如果这个程序对我来说可有可无,提示我也不会去更新它,我觉的检测时机可以放在我需要用到这个功能的时候,提示用户更新,那么我相信,这种更新的几率就大很多。言归正传,我们就谈一下具体应该怎么去做。首先,我们需要知道两个版本号。1: 苹果商店版本(APP Store)查询苹果商店版本,我们首先的有一个地址去查询http://itunes.apple.com/lookup?id = 你的应用程序的ID(有9位有10位)具体怎么发请求,解析数据,这个童鞋们都明白,我们直接看数据{    

      resultCount = 1;

      results =(                 

               {advisories = (         );            

                artistId =   开发者 ID;            

                artistName =   开发者名称;            

                artistViewUrl =            

                artworkUrl100 =            

                artworkUrl512 =            

                artworkUrl60 =            

                bundleId =            

                contentAdvisoryRating =            

                currency =            

                currentVersionReleaseDate =            

                description =            

                features =            

                fileSizeBytes =            

                formattedPrice =            

                genreIds =            

                genres =            

                ipadScreenshotUrls =            

                isGameCenterEnabled =            

                isVppDeviceBasedLicensingEnabled =            

                kind =            

                languageCodesISO2A =            

                minimumOsVersion =            

                price =            

                primaryGenreId =            

                primaryGenreName =            

                releaseDate =            

                releaseNotes =  更新内容            

                screenshotUrls =            

                sellerName =            

                sellerUrl =            

                supportedDevices =            

                trackCensoredName =            

                trackContentRating =            

                trackId =  应用程序 ID;            

                trackName =            

                trackViewUrl =            

                version =  版本号;            

                wrapperType =              }    );}

 *

 *  @since <#1.0#>

 */



- (void)viewDidLoad {
    [super viewDidLoad];

    //我们需要的信息一般有 version(版本号)  releaseNotes(更新的内容)
    //2: 正在应用的版本
    NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
    NSString* versionNum = [infoDict objectForKey:@"CFBundleVersion"];
    
    
    //我们得到两个版本之后,剩下的事情就是比较了。现在版本号一般也都是3位,例如:1.0.0  那么我们就不能转换为双精度去比较,我们来试试其他方法:
    
    
    NSURL *APPUrl = [NSURL URLWithString:GetAppInfoUrl];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSURLRequest *request = [NSURLRequest requestWithURL:APPUrl];
    NSURLSessionDataTask *task =  [session  dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"APP商店信息-------%@",dic);
        int resultCount = [[dic objectForKey:@"resultCount"] intValue];
        if(resultCount > 0){
            NSArray *infoArray = [dic objectForKey:@"results"];
            NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
            NSString *appStoreVersion = [releaseInfo objectForKey:@"version"];
            if(infoArray && releaseInfo && appStoreVersion)
            {
                NSString *note = [releaseInfo objectForKey:@"releaseNotes"];
                //获取当前的版本型号
                NSString *localVerson = nil;
                
                if ([appStoreVersion compare:localVerson options:NSNumericSearch] == NSOrderedDescending){
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"有新版本了!赶快更新吧,第一时间体验新功能!"
                                                                     message:note//更新内容
                                                                    delegate:self
                                                           cancelButtonTitle:@"稍后"
                                                           otherButtonTitles:@"更新",nil];
                        [av show];
                    });
                }
            }
        }
    }]; 
    [task resume];
    
    

    
    //这里有个点要提醒一下童鞋们,提示框要回到主线程哦!如果有更新,那么我们就去更新!
    // APPDownloadURL : 项目下载地址
    NSURL *url = [NSURL URLWithString:@"APP的下载地址"];
    [[UIApplication sharedApplication] openURL:url];
    
    
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值