iOS版本检测与版本升级

14年苹果官方要求所有的APP不能出现 “当前版本”字样,是因为从iOS8系统开始,你可以在设置里面设置在WiFi情况下,自动更新安装的APP。此功能大大方便了用户,但是一些用户没有开 启此项功能,因此还是需要在程序里面提示用户的。方法一是在服务器接口约定对应的数据,这样,服务器直接传递信息,提示用户有新版本,可以去商店升级。方 法二是检测手机上安装的APP的版本,然后跟AppStore上app的版本信息联合来判断。

方法一中,之前的做法是在特定的页面的时候 发送请求,根据请求数据,弹出 UIAlertView进行提示。


方法二也是比较常用的方法。
首先,确定安装的APP的版本。
当前运行版本信息可以通过info.plist文件中的bundle version中获取

    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];

其次,请求APP的相关数据
https://itunes.apple.com/lookup?id=XXX    (其中XXX是你的app的商店 ID)
,你获取到数据是json数据。
进行解析,将 version 的数据跟你从info.plist 获取的数据进行比较!

trackCensoredName = 审查名称;trackContentRating = 评级;trackId = 应用程序 ID;trackName = 应用程序名称;trackViewUrl = 应用程序介绍网址;userRatingCount = 用户评级;userRatingCountForCurrentVersion = 1;version = 版本号

 

-(void)judgeAPPVersion
{
//    https://itunes.apple.com/lookup?id=604685049
   
    NSString *urlStr = @"https://itunes.apple.com/lookup?id=604685049";
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    [NSURLConnection connectionWithRequest:req delegate:self];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
   
    NSError *error;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
    NSDictionary *appInfo = (NSDictionary *)jsonObject;
    NSArray *infoContent = [appInfo objectForKey:@"results"];
    NSString *version = [[infoContent objectAtIndex:0] objectForKey:@"version"];
   
     NSLog(@"商店的版本是 %@",version);
   
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
    NSLog(@"当前的版本是 %@",currentVersion);

 
    if (![version isEqualToString:currentVersion]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"商店有最新版本了" delegate:nil cancelButtonTitle:@"YES" otherButtonTitles:nil,nil];
        [alert show];
    }
   
}

当然你还是可以在页面有个跳转到商店更新APP的接口的按钮滴!
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:trackViewUrl]];

 

posted @ 2016-05-16 13:45  MakeKeyReuseable  阅读(354)  评论(0编辑  收藏  举报