虽然苹果官方是不允许应用自动检测更新,提示用户下载,因为苹果会提示你有多少个软件需要更新,但是有的时候提示用户一下有新版还是很有必要的。

首先说一下原理:

每个上架的苹果应用程序,都会有一个应用程序的ID,根据这个ID我们就可以获取到当前程序的最新版本号,然后和自己的版本号作比较,如果一样的话就是最新版,反之就不是新版,就可以提示用户来手动下载最新版的程序。因为有ID所以就可以定位到这个APP,点击下载即可。

源码:

一般建议检测更新的代码放到主页控制器里。

首先还要导入一个头文件用来打开AppStore下载更新

//AppStore
#import <StoreKit/StoreKit.h>

接着还有代理

SKStoreProductViewControllerDelegate

然后开始检测更新

//检测版本,版本更新
NSError *error;
NSString *urlStr = @"http://itunes.apple.com/lookup?id=上架AppID";
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary *appInfoDict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
if (error)
{
return;
}
NSArray *resultArray = [appInfoDict objectForKey:@"results"];
if (![resultArray count])
{
return;
} NSDictionary *infoDict = [resultArray objectAtIndex:];
//获取服务器上应用的最新版本号
NSArray* arr=[infoDict[@"version"] componentsSeparatedByString:@"."];
NSInteger updateVersion=;
for (int i=; i<arr.count; i++)
{
if(i==)
{
updateVersion+=[arr[i] integerValue]*;
}
else if (i==)
{
updateVersion+=[arr[i] integerValue]*;
}
else if (i==)
{
updateVersion+=[arr[i] integerValue]*;
}
else if (i==)
{
updateVersion+=[arr[i] integerValue]*;
}
}
NSString *trackName = infoDict[@"trackName"];
_trackViewUrl = infoDict[@"trackViewUrl"];
//获取当前设备中应用的版本号
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSArray* arr2=[[infoDic objectForKey:@"CFBundleShortVersionString"] componentsSeparatedByString:@"."];
NSInteger currentVersion=; for (int i=; i<arr2.count; i++)
{
if(i==)
{
currentVersion+=[arr2[i] integerValue]*;
}
else if (i==)
{
currentVersion+=[arr2[i] integerValue]*;
}
else if (i==)
{
currentVersion+=[arr2[i] integerValue]*;
}
else if (i==)
{
currentVersion+=[arr2[i] integerValue]*;
}
} //判断两个版本是否相同
if (currentVersion < updateVersion)
{
NSString *titleStr = [NSString stringWithFormat:@"检查更新:%@", trackName];
NSString *messageStr = [NSString stringWithFormat:@"发现新版本%@,是否更新", infoDict[@"version"]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:titleStr message:messageStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"升级", nil];
     //为了区分其他弹出框而已
alert.tag = ;
[alert show]; }

接着就是用户更不更的问题了

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (alertView.tag == )
{
if (buttonIndex == )
{
//点击”升级“按钮,就从打开app store上应用的详情页面
SKStoreProductViewController *storeProductVC = [[SKStoreProductViewController alloc] init];
storeProductVC.delegate = self;
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"上架AppID" forKey:SKStoreProductParameterITunesItemIdentifier];
[storeProductVC loadProductWithParameters:dict completionBlock:^(BOOL result, NSError *error)
{
if (result)
{
[self presentViewController:storeProductVC animated:YES completion:nil];
}
}];
}
}
}

还有就是用户打开AppStore但是没有下载就返回回来的状况

- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController
{
[viewController dismissViewControllerAnimated:YES completion:nil];
}

OK到这里就结束了。这样的话就可以检测App是不是最新版了,而且用户也能实时看到,最关键的是苹果审核还能通过。

iOS自动检测版本更新的更多相关文章

  1. iOS开发 检测版本更新

    iOS开发 检测版本更新的实现 苹果给了我们一个接口,能根据应用id请求一些关于应用的信息.我们可以根据返回的信息,来判断版本是否和应用的版本一致,如果不一致,那么就出现新的版本了.这时,就需要向用户 ...

  2. iOS内存泄漏自动检测工具PLeakSniffer

    新款objective-C内存泄漏自动检测工具 PLeakSniffer , GitHub地址 (https://github.com/music4kid/PLeakSniffer). 背景 前些天读 ...

  3. iOS子线程操作检测版本更新,有新版本通知用户更新, CheckVersion

    iOS子线程操作检测版本更新,有新版本通知用户更新 CheckVersion 一:如何使用: #import "CheckVersion.h" //输入你的app在appStore ...

  4. iOS 检测版本更新(02)

    iOS 检测版本更新 如果我们要检测app版本的更新,那么我们必须获取当前运行app版本的版本信息和appstore 上发布的最新版本的信息. 当前运行版本信息可以通过info.plist文件中的bu ...

  5. IOS开发中如何实现自动检测更新APP

    自动检测更新实现逻辑: 先上github地址:https://github.com/wolfhous/HSUpdateApp 1,获取当前项目APP版本号 2,拿到AppStore项目版本号 3,对比 ...

  6. 检测版本更新,iOS

    检测版本更新的方法. //检查新版本 更新 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ), ^ ...

  7. iOS 覆盖率检测原理与增量代码测试覆盖率工具实现

    背景 对苹果开发者而言,由于平台审核周期较长,客户端代码导致的线上问题影响时间往往比较久.如果在开发.测试阶段能够提前暴露问题,就有助于避免线上事故的发生.代码覆盖率检测正是帮助开发.测试同学提前发现 ...

  8. 上传IOS项目和版本更新流程图

    上传IOS项目和版本更新流程图 必备IDP证书和distribution证书(第一个证书是真机部署测试时用到的,后者证书是发布时需要用到的,缺一不可). 我就说说接下来应该做的流程.在你保证拥有以上两 ...

  9. 【译】Spring 4 自动装配、自动检测、组件扫描示例

    前言 译文链接:http://websystique.com/spring/spring-auto-detection-autowire-component-scanning-example-with ...

随机推荐

  1. C#调用百度高精度IP定位API通过IP获取地址

    API首页:http://lbsyun.baidu.com/index.php?title=webapi/high-acc-ip 1.申请百度账号,创建应用,获取密钥(AK) http://lbsyu ...

  2. UVA 624CD(01背包输出 + 输出路径)

    You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is o ...

  3. 计算机视觉之《OpenCV开发环境搭建》

    codeblock安装:http://blog.csdn.net/hitwengqi/article/details/7985343 ubuntu+codeblock+opencv:http://bl ...

  4. Summary - SNMP Tutorial

    30.13 Summary Network management protocols allow a manager to monitor and control routers and hosts. ...

  5. highcharts总结

    //tooptip此属性表达的是鼠标移那个位置显示的值 tooptip:{ formatter: function (){ //this.point.name 表达显示名字 //this.x 表达显示 ...

  6. Window系统性能获取帮助类

    前言: 这个是获取Windows系统的一些性能的帮助类,其中有:系统内存.硬盘.CPU.网络(个人测试还是比较准的).Ping.单个进程的内存.Cpu.网络(不准).    最初在这个的时候在各种搜索 ...

  7. 关于MapReduce中自定义分组类(三)

    Job类  /**    * Define the comparator that controls which keys are grouped together    * for a single ...

  8. Markdown段落首行缩进的实现办法

    添加:   

  9. 日期格式转换 java 2016-09-03T00:00:00.000+08:00

    /**  * 日期格式转换yyyy-MM-dd'T'HH:mm:ss.SSSXXX  (yyyy-MM-dd'T'HH:mm:ss.SSSZ) TO  yyyy-MM-dd HH:mm:ss  * @ ...

  10. Wrong list

    1.背包dp[i][j]无论当前物品是否不大于j都可以转移dp[i-1][j] 2.循环从0开始还是1开始的问题 3.无向图边集数组开两倍 4.(3*987654321) > maxint 4. ...