IOS判断app在appstore是否有可用的更新

iTunes可以提供app的版本信息,主要通过appid获取,如 http://itunes.apple.com/lookup?id=950918119,使用时只需要到iTunes查找自己的appid,修改成自己的appid即可

使用HTTP模式读取此链接可以获取app信息的json字符串


-(void)checkVersion
{
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]];//strURL为你的appid地址
[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request startAsynchronous];
}

-(void)requestFinished:(ASIHTTPRequest *)request
{
NSString *recStr = [[NSString alloc] initWithData:request.responseData encoding:NSUTF8StringEncoding];
recStr = [recStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];//返回的字符串有前面有很多换行符,需要去除一下
NSDictionary *resultDic = [JSONHelper DeserializerDictionary:recStr];//jsonhelper是我封装的json解析类,你可以使用自己方式解析

NSArray *infoArray = [resultDic objectForKey:@"results"];
if (infoArray.count > 0) {

NSDictionary* releaseInfo =[infoArray objectAtIndex:0];
NSString* appStoreVersion = [releaseInfo objectForKey:@"version"];
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];

NSArray *curVerArr = [currentVersion componentsSeparatedByString:@"."];
NSArray *appstoreVerArr = [appStoreVersion componentsSeparatedByString:@"."];
BOOL needUpdate = NO;
//比较版本号大小
int maxv = (int)MAX(curVerArr.count, appstoreVerArr.count);
int cver = 0;
int aver = 0;
for (int i = 0; i < maxv; i++) {
if (appstoreVerArr.count > i) {
aver = [NSString stringWithFormat:@"%@",appstoreVerArr[i]].intValue;
}
else{
aver = 0;
}
if (curVerArr.count > i) {
cver = [NSString stringWithFormat:@"%@",curVerArr[i]].intValue;
}
else{
cver = 0;
}
if (aver > cver) {
needUpdate = YES;
break;
}
}

//如果有可用的更新
if (needUpdate){

trackViewURL = [[NSString alloc] initWithString:[releaseInfo objectForKey:@"trackViewUrl"]];//trackViewURL临时变量存储app下载地址,可以让app跳转到appstore
UIAlertView* alertview =[[UIAlertView alloc] initWithTitle:@"版本升级" message:[NSString stringWithFormat:@"发现有新版本,是否升级?"] delegate:self cancelButtonTitle:@"暂不升级" otherButtonTitles:@"马上升级", nil];
[alertview show];

}else{
[self showAlertView:@"已经是最新版本了"];
}


}
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1){
UIApplication *application = [UIApplication sharedApplication];
[application openURL:[NSURL URLWithString:trackViewURL]];
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值