<span style="color:#FF0000;">1. 代码自己理解</span>
/**
* 检测软件是否需要升级
*/
-(void)checkVersion
{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%i",iFeverAPPID]];
ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:url];
[request setUseCookiePersistence:YES];
[request setDelegate: self];
[request setDidFailSelector:@selector(getVersionRequestFailed:)];
[request setDidFinishSelector:@selector(getVersionRequestSuccess:)];
[request startAsynchronous];//开始异步请求
}
-(void)getVersionRequestFailed:(ASIHTTPRequest *)request1
{
NSLog(@"从AppStore获取版本信息失败!!");
}
-(void)getVersionRequestSuccess:(ASIHTTPRequest *)request1
{
NSString *newVersion;
NSData *responseData = [request1 responseData];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:nil];
NSArray *resultArray = [dic objectForKey:@"results"];
for (id config in resultArray) {
newVersion = [config valueForKey:@"version"];
}
if (newVersion) {
NSLog(@"通过AppStore获取的版本号是:%@",newVersion);
}
//获取本地版本号
NSString *localVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleVersion"];
NSString *msg = [NSString stringWithFormat:@"你当前的版本是V%@,发现新版本V%@,是否下载新版本?",localVersion,newVersion];
if ([newVersion floatValue] > [localVersion floatValue]) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"升级提示!" message:msg delegate:self cancelButtonTitle:@"下次再说" otherButtonTitles:@"现在升级", nil];
alert.tag = kVersionNeedUpdateAlertTag;
[alert show];
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == kVersionNeedUpdateAlertTag) {
//软件需要更新提醒
if (buttonIndex == 1) {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/cn/app/wan-zhuan-quan-cheng/id%i?mt=8",iFeverAPPID]];
[[UIApplication sharedApplication]openURL:url];
/*
// 打开iTunes 方法二:此方法总是提示“无法连接到itunes”,不推荐使用
NSString *iTunesLink = @"itms-apps://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftwareUpdate?id=%i&mt=8";
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"itms-apps://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftwareUpdate?id=%i&mt=8",iFeverAPPID]];
[[UIApplication sharedApplication] openURL:url];
*/
}
}
}
2.注意事项
- APPID: 你的应用程序的ID 是 itunes connect里的 Apple ID
- 上面的网络请求是用的ASI的,我们都知道ASI已经停止维护了,所以,你们可以自己选择自己项目中的网络请求.
参考来自:http://cache.baiducontent.com/c?m=9d78d513d9981cee4fece4697d62c0156a43f0662ba4d0027fa5c00ad1735a315016e9ac53530772d1d20d6715e80902e5aa7034751421c486d5cd0f98ac925f69cf7c652d41d85612a448f2945b768166cb0bb7f858fadeb036f7f68d&p=8b2a970792b107f243bd9b7d0d128f&newp=8b2a970e9d934eac58e9d525565798231610db2151d2d45e34&user=baidu&fm=sc&query=iOS%BF%AA%B7%A2+app%B8%FC%D0%C2&qid=a3583c060000a36b&p1=2