1、版本检测代码:
-(void)checkVersion
{
//当前版本号
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [infoDic objectForKey:@"CFBundleVersion"];
//向AppStore发送同步请求
NSString *URL = @"http://itunes.apple.com/lookup?id=752875885";//你的AppID
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 = [[CJSONDeserializer deserializer] deserializeAsDictionary:recervedData error:&error] ;
NSArray *infoArray = [dic objectForKey:@"results"];
if ([infoArray count]) {
NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
//得到最新appstore上的版本号
NSString *lastVersion = [releaseInfo objectForKey:@"version"];
//如果当前版本与appstore上的版本不一样
if (![lastVersion isEqualToString:currentVersion]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"有新的版本,是否前往更新?" delegate:self cancelButtonTitle:@"不更新" otherButtonTitles:@"更新", nil];
alert.tag = 1000;
[alert show];
}
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag==10000) {
if (buttonIndex==1) {
//跳转到appstore的下载页
NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/yourappname/idxxxxxx?mt=8"];
[[UIApplication sharedApplication]openURL:url];
}
}
}
2、如何设置当前工程app的版本号
(1)只要简单地设置version和build,其他地方(比如Info.plist)的版本号都会随之改变。可以简单地将version和build设置成一样的。
至于二者的差别可以参考此文:http://blog.csdn.net/kafeidev/article/details/8221273
(2)上面的代码 NSString *currentVersion = [infoDic objectForKey:@"CFBundleVersion"];取出来的build的内容
3、如何知道app在App Store上的ID
(1)在App Store里搜索出你的app,然后右击“拷贝链接”,就可以得到app的下载地址:https://itunes.apple.com/cn/app/ju-duan/idxxxxxx?mt=8,id后面跟的内容就是你的appid。
(2)登录开发者账号,进入“manage your apps”也可找到对应的app的apple id