聯系人:石虎 QQ:1224614774 昵稱: 嗡嘛呢叭咪哄
QQ群:807236138 群稱: iOS 技術交流學習群
一、實作思路
/** 實作思路
1.擷取目前項目APP版本号
2.拿到AppStore項目版本号
3.對比版本号,實作更新功能
*/
//一定要先配置自己項目在商店的APPID,配置完最好在真機上運作才能看到完全效果哦
#define STOREAPPID @"1234567890"
二、代碼實作
//檢測app更新
-(void)updateApp
{
//1.先擷取目前工程項目版本号
NSDictionary *infoDic = [[NSBundlemainBundle]infoDictionary];
NSString *currentVersion = infoDic[@"CFBundleShortVersionString"];
//2.從網絡擷取appStore版本号
NSError *error;
NSData *response = [NSURLConnectionsendSynchronousRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:[NSStringstringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",STOREAPPID]]]returningResponse:nilerror:nil];
//2.1沒有内容
if (response == nil) {
NSLog(@"你沒有連接配接網絡哦");
return;
}
//3.序列化解析
NSDictionary *appInfoDic = [NSJSONSerializationJSONObjectWithData:responseoptions:NSJSONReadingMutableLeaveserror:&error];
//3.1資料錯誤
if (error) {
NSLog(@"hsUpdateAppError:%@",error);
return;
}
//3.2字典解析
NSArray *array = appInfoDic[@"results"];
NSDictionary *dic = array[0];
NSString *appStoreVersion = dic[@"version"];
//列印版本号
NSLog(@"目前版本号:%@\n商店版本号:%@",currentVersion,appStoreVersion);
//4.目前版本号小于商店版本号,就更新
if([currentVersion floatValue] < [appStoreVersion floatValue])
{
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"版本有更新" message:[NSStringstringWithFormat:@"檢測到新版本(%@),是否更新?",appStoreVersion] delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"更新",nil];
[alert show];
}else{
NSLog(@"檢測到不需要更新");
}
}