1.必定要先配置本身項目在商店的APPID,配置完最好在真機上運行才能看到徹底效果網絡
2.獲取當前工程項目版本號app
3.從網絡獲取appStore版本號orm
4.當前版本號小於商店版本號,就更新string
代碼以下:it
//商店的APPIDio
#define STOREAPPID@"1080182980"table
-(void)UpdateApp配置
{date
//2先獲取當前工程項目版本號request
NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
NSString*currentVersion=infoDic[@"CFBundleShortVersionString"];
//3從網絡獲取appStore版本號
NSError *error;
NSData *response = [NSURLConnection
sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",STOREAPPID]]]
returningResponse:nil error:nil];
if (response == nil) {
NSLog(@"你沒有鏈接網絡哦");
return;
}
NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:response
options:NSJSONReadingMutableLeaves error:&error];
if (error) {
NSLog(@"hsUpdateAppError:%@",error);
return;
}
NSArray *array = appInfoDic[@"results"];
NSDictionary *dic = array[0];
NSString *appStoreVersion = dic[@"version"];
//打印版本號
NSLog(@"當前版本號:%@\n商店版本號:%@",currentVersion,appStoreVersion);
//4當前版本號小於商店版本號,就更新
if([currentVersion floatValue] <[appStoreVersion floatValue])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"版本有更新"
message:[NSString stringWithFormat:@"檢測到新版本(%@),是否更新?",appStoreVersion] delegate:self cancelButtonTitle:@"取消"otherButtonTitles:@"更新",nil];
[alert show];
}else{
NSLog(@"版本號好像比商店大噢!檢測到不須要更新");
}
}