實(shí)現(xiàn)思路
http://www.cnblogs.com/jys509/p/5390505.html
- 1.獲取當(dāng)前項(xiàng)目APP版本號(hào)
- 2.拿到AppStore項(xiàng)目版本號(hào)
- 3.對(duì)比版本號(hào),實(shí)現(xiàn)更新功能
#import
"ViewController.h"
//1一定要先配置自己項(xiàng)目在商店的APPID,配置完最好在真機(jī)上運(yùn)行才能看到完全效果哦
#define STOREAPPID
@"1080182980"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//一句代碼實(shí)現(xiàn)檢測(cè)更新
[self hsUpdateApp];
}
/**
* 天朝專用檢測(cè)app更新
*/
-(void)hsUpdateApp
{
//2先獲取當(dāng)前工程項(xiàng)目版本號(hào)
NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
NSString*currentVersion=infoDic[@"CFBundleShortVersionString"];
//3從網(wǎng)絡(luò)獲取appStore版本號(hào)
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(@"你沒有連接網(wǎng)絡(luò)哦");
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"];
//打印版本號(hào)
NSLog(@"當(dāng)前版本號(hào):%@\n商店版本號(hào):%@",currentVersion,appStoreVersion);
//4當(dāng)前版本號(hào)小于商店版本號(hào),就更新
if([currentVersion floatValue] <[appStoreVersion floatValue])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"版本有更新"
message:[NSString stringWithFormat:@"檢測(cè)到新版本(%@),是否更新?",appStoreVersion] delegate:self cancelButtonTitle:@"取消"otherButtonTitles:@"更新",nil];
[alert show];
}else{
NSLog(@"版本號(hào)好像比商店大噢!檢測(cè)到不需要更新");
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者