新的iOS版本更新逻辑和旧的没有太大的区别,只是多了一个build和version的区别。
1.关于Version和Build
Version说白了就是版本号,也是提交到itunes的版本号,也就是请求http://itunes.apple.com/lookup?id=xxx返回的版本号
Build呢是用于在APP被拒之后递增一个版本时所用,截个图,说明问题:
1.1 比如我在submit之前在Xcode中设置的:
1.2 那么你submit之后在https://itunesconnect.apple.com看到的将是:
这样说,大家明白吧?
2.版本更新变化
由于Version和Build的区别,所以原来获取当前APP版本的方法:
[[[NSBundlemainBundle]infoDictionary]objectForKey:@"CFBundleVersion"]
获取到的将是Build的版本号
所以正确的方法是:
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
3.附带完整的版本更新方法如下
3.1 检查更新
[KVParserClient checkNewVersion:^(BOOL flag, NSError *error) {
[SVProgressHUD dismiss];
if (error) {
[SVProgressHUD showErrorWithStatus:[error localizedDescription]];
}else if (flag == false) {
//[SVProgressHUD showSuccessWithStatus:@"已是最新版本"];
}else if (flag == true) {
dispatch_async(dispatch_get_main_queue(), ^{
[self UPdateNewVersion];
});
}
}];
- (void)UPdateNewVersion
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"发现新版本" message:@"是否前往App Store更新?" delegate:self cancelButtonTitle:@"稍候" otherButtonTitles:@"确认", nil];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==0) {
}else if (buttonIndex==1) {
NSString *iTunesLink = [NSString stringWithFormat:@"http://itunes.apple.com/cn/app/id%@?mt=8",ITUNESAPPID];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesLink]];
}
}
3.2 具体更新请求方法
#define ITUNESAPPID @"xxxx"
typedef void (^CheckNewVersionHandler)(BOOL flag, NSError *error);
/*
*判断是否是最新版
*/
+(void)checkNewVersion:(CheckNewVersionHandler )checkBlock
{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",ITUNESAPPID]];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (!connectionError) {
NSString *version = @"";
NSDictionary *loginAuthenticationResponse = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves|NSJSONReadingAllowFragments error:nil];
NSArray *configData = [loginAuthenticationResponse valueForKey:@"results"];
if (configData&&[configData count]>0) {
for (id config in configData)
{
version = [config valueForKey:@"version"];
}
if ([version floatValue] > [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]floatValue])
{
checkBlock(true,nil);
}
}
}
checkBlock(false,connectionError);
}];
}