美文网首页
iOS开发 判断当前APP版本和升级

iOS开发 判断当前APP版本和升级

作者: 庚妖素 | 来源:发表于2017-07-03 14:19 被阅读0次

从iOS8系统开始,用户可以在设置里面设置在WiFi环境下,自动更新安装的App。此功能大大方便了用户,但是一些用户没有开启此项功能,因此还是需要在程序里面提示用户的

方法一:在服务器接口约定对应的数据,这样,服务器直接传递信息,提示用户有新版本,可以去商店升级

注意:这个方法是有毛病的,若您的App还没审核通过,而移动端后台数据已经更新,后台给您返回的版本号是最新的版本号,老版本会提示用户升级,但是用户点击升级后跳转至AppStore却发现App还未更新

方法二:检测手机上安装的App版本,然后跟App Store上App的版本信息联合来判断(目前最常用的方法)

步骤一:获取当前运行的版本信息,通过info.plist文件的bundle version中获取

NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];

//当前版本号

NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];

NSLog(@"当前版本号%@",currentVersion);

步骤二:获取AppStore中的App版本信息

http://itunes.apple.com/search?term=你的应用程序名称&entity=software

-(void)judgeAppVersion{

//AppStore访问地址(重点)

//trackId = 应用程序 ID;

NSString *urlStr = @"https://itunes.apple.com//lookup?id=trackId";

NSURL *url = [NSURL URLWithString:urlStr];

NSURLRequest *req = [NSURLRequest requestWithURL:url];

[NSURLConnection connectionWithRequest:req delegate:self];

}

#pragma mark - NSURLConnectionDataDelegate

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

NSError *error;

//解析

NSDictionary *appInfo = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

NSArray *infoContent = [appInfo objectForKey:@"results"];

//最新版本号

NSString *version = [[infoContent objectAtIndex:0] objectForKey:@"version"];

NSLog(@"最新版本号%@",version);

NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];

//当前版本号

NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];

if (![version isEqualToString:currentVersion]) {

UIAlertView *arl = [[UIAlertView alloc]initWithTitle:nil message:@"商城里面有新版本" delegate:nil cancelButtonTitle:@"YES" otherButtonTitles:nil, nil];

[arl show];

//trackViewUrl = 应用程序介绍网址;

[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"trackViewUrl"]];

}

}

相关文章

网友评论

      本文标题:iOS开发 判断当前APP版本和升级

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