APP检查版本更新

一、检测更新

-(void)checkAppUpdate{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
NSDictionary*infoDict = [[NSBundlemainBundle]infoDictionary];
NSString*nowVersion = [infoDictobjectForKey:@"CFBundleShortVersionString"];
NSURL*url = [NSURLURLWithString:[NSStringstringWithFormat:@"http://itunes.apple.com/lookup?id=%@",kStoreAppId]];
NSData*data = [NSDatadataWithContentsOfURL:url];
if(data.length==0){
return;
}
NSDictionary*dic = [NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingAllowFragmentserror:nil];
NSArray*array = [dicobjectForKey:@"results"];
if(array.count==0){
return;
}
NSDictionary*info = array[0];
NSString*newVersion = [infoobjectForKey:@"version"];
NSString*releaseNotes = [infoobjectForKey:@"releaseNotes"];
if([nowVersioncompare:newVersionoptions:NSNumericSearch] ==NSOrderedAscending){
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView*alert = [[UIAlertViewalloc]initWithTitle:@"版本有更新"message:nildelegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"更新",nil];
alert.message= releaseNotes;
alert.tag=101;
[alertshow];
});
}
});
}

二 、UIAlertViewDelegate代理

- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag==101&& buttonIndex ==1){
//此处加入应用在app store的地址,方便用户去更新,一种实现方式如下:
NSURL*url = [NSURLURLWithString:[NSStringstringWithFormat:@"https://itunes.apple.com/cn/app/id%@?ls=1&mt=8",kStoreAppId]];
[[UIApplicationsharedApplication]openURL:url];
}
}

你可能感兴趣的:(APP检查版本更新)