您的位置:首页 > 移动开发

iOS App的版本检测更新功能(最新版)

2014-08-22 12:04 417 查看
今天是真正的想写一篇博客,以前都是工作忙没时间,所以从来没认真写过,就从今天起吧!

(由于上传突然断网,博主又重新谢了一遍....)

下面是关于版本检测更新的介绍:

首先,我们要获得等待上传或正在写的app项目的版本号显示在view上

NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
CFShow((__bridge CFTypeRef)(infoDic));
NSString *appVersion = [infoDic objectForKey:@"CFBundleVersion"];
我们得到的NSString就是版本号了

然后我们要获得上个版本在app store 上的信息:

//检查是否需要更新
-(void)examineAppUpdate
{
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
//CFShow((__bridge CFTypeRef)(infoDic));
NSString *currentVersion = [infoDic objectForKey:@"CFBundleVersion"];

NSString *URL = @"http://itunes.apple.com/lookup?id=<span style="color:#cc33cc;">ID号</span>";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:URL]];
[request setHTTPMethod:@"POST"];
NSHTTPURLResponse *urlResponse = nil;
NSError *error = nil;
NSData *recervedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:recervedData options:NSJSONReadingMutableContainers error:nil];
NSLog(@"dic = %@",dic);

然后我们通过json解析得到的数据为:

);
trackCensoredName = "\U6367\U8179\U7b11\U8bdd";
trackContentRating = "17+";
trackId = 493760196;
trackName = "\U6367\U8179\U7b11\U8bdd";
trackViewUrl = "https://itunes.apple.com/us/app/peng-fu-xiao-hua/id<span style="color:#ff0000;">APP的id</span>?mt=8&uo=4";
userRatingCount = 5;
userRatingCountForCurrentVersion = 3;
version = "V1.08";
wrapperType = software;
}
我们就得到了我们要的app的信息,然后继上边的dic之后进行比较和操作:
NSArray *infoArray = [dic objectForKey:@"results"];
if ([infoArray count]) {
NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
NSString *lastVersion = [releaseInfo objectForKey:@"version"];
NSLog(@"%@",lastVersion);

if (![lastVersion isEqualToString:currentVersion]) {
//trackViewURL = [releaseInfo objectForKey:@"trackVireUrl"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"有新的版本更新,是否前往更新?" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"更新", nil];
alert.tag = 10000;
[alert show];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"此版本为最新版本" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
alert.tag = 10001;
[alert show];
}
}
最后点击确定进入此app所在的app store上的页面:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag==10000) {
if (buttonIndex==1) {
NSURL *url = [NSURL URLWithString:@"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=<span style="color:#cc0000;">app的ID</span>"];
[[UIApplication sharedApplication]openURL:url];
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: