iOS 检测更新的实现
来源:互联网 发布:洪荒之力网络意思 编辑:程序博客网 时间:2024/06/07 19:00
/*
第一步: 根据应用名称搜索应用,然后根据应用绑定的ID在结果中筛选出我们要找的应用,并取出应用的AppID
*/
- (void)getAppID {
processView = [[UIProcessViewalloc]initWithFrame:self.view.framewithText:@"检测中..."];
[processViewstartAnimating];
[self.viewaddSubview:processView];
[SVHTTPRequestPOST:@"http://itunes.apple.com/search"
parameters:[[NSDictionaryalloc] initWithObjectsAndKeys:APP_NAME,@"term",@"software",@"entity",nil]
completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
if (!error&&[urlResponse statusCode]==200) {
NSData *data = (NSData *)response;
id res = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingMutableContainerserror:nil];
NSLog(@"res.class==%@",[res class]);
NSLog(@"res == %@",res);
NSLog(@"results class == %@",[[res objectForKey:@"results"]class]);
NSArray *arr = [res objectForKey:@"results"];
for (id config in arr)
{
NSString *bundle_id = [config valueForKey:@"bundleId"];
if ([bundle_id isEqualToString:APP_BUNDLE_IDENTIFIER]) {
[processView stopAnimating];
app_id = [config valueForKey:@"trackId"];
updateURL = [config valueForKey:@"trackViewUrl"];
NSString *app_Name = [config valueForKey:@"trackName"];
NSString *version = [config valueForKey:@"version"];
NSLog(@"app_id == %@,app_Name == %@,version == %@",app_id,app_Name,version);
[self checkUpdate:version];
}
}
} else {
[processView stopAnimating];
[CTCommonUtilsshowAlertViewOnView:self.viewwithText:@"检测失败,当前无网络连接!"];
}
}];
}
/*
第二步:通过比较从App Store获取的应用版本与当前程序中设定的版本是否一致,然后判断版本是否有更新
*/
- (void)checkUpdate:(NSString *)versionFromAppStroe {
NSDictionary *infoDict = [[NSBundlemainBundle] infoDictionary];
NSString *nowVersion = [infoDict objectForKey:@"CFBundleVersion"];
NSLog(@"nowVersion == %@",nowVersion);
[processViewstopAnimating];
//检查当前版本与appstore的版本是否一致
if (![versionFromAppStroe isEqualToString:nowVersion])
{
UIAlertView *createUserResponseAlert = [[UIAlertView alloc] initWithTitle:@"提示" message: @"有新的版本可供下载" delegate:self cancelButtonTitle:@"下次再说" otherButtonTitles:@"去下载", nil];
[createUserResponseAlert show];
} else {
[CTCommonUtilsshowAlertViewOnView:self.viewwithText:@"暂无新版本"];
}
}
#pragma mark - AertView delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
//去appstore中更新
//方法一:根据应用的id打开appstore,并跳转到应用下载页面
//NSString *appStoreLink = [NSString stringWithFormat:@"http://itunes.apple.com/cn/app/id%@",app_id];
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString:appStoreLink]];
//方法二:直接通过获取到的url打开应用在appstore,并跳转到应用下载页面
[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:updateURL]];
} else if (buttonIndex == 2) {
//去itunes中更新
[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:@"itms://itunes.apple.com/cn/app/guang-dian-bi-zhi/id511587202?mt=8"]];
}
}
之前项目需要用到app自动更新的功能,现将实现方案分享出来。
iOS程序自动提示更新的实现方案大致分为两种:
第一种,自己服务器提供一个接口,告知相关app的当前版本,是否需要更新,以及更新的地址等信息 。
第二种,就是利用苹果的appstore 提供的相关api进行查询更新。
由于此前没有找到iOS程序更新的方法,就用了第一种方式,但后来发现了一些问题,自己提供服务器,需要维护,程序提交更新后,由于苹果需要审核,中间会有时间差,这个时间不好把握。后台就找苹果的相关文档,终于找到了。
http://www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.html
实现机制:
#define APP_URL @"http://itunes.apple.com/lookup?id=你程序的appId"
ASIFormDataRequest *formRequst = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:APP_URL]];
请求网络数据,返回的大致数据如下,其他还有好多数据,我们把关键的给截取出来
{
resultCount = 1;
results = (
{
artistId = 开发者 ID;
artistName = 开发者名称;
price = 0;
isGameCenterEnabled = 0;
kind = software;
languageCodesISO2A = (
EN
);
trackCensoredName = 审查名称;
trackContentRating = 评级;
trackId = 应用程序 ID;
trackName = 应用程序名称";
trackViewUrl = 应用程序介绍网址;
userRatingCount = 用户评级;
userRatingCountForCurrentVersion = 1;
version = 版本号;
wrapperType = software;
}
);
}
取得这些数据后关键的信息就是“ version”最新版本号和“ trackViewUrl”程序地址。然后与本地程序的版本比较即可。
上面的build 对应info.plist文件里的“Bundle version”字段,供程序调试用,即内部调试版本号,不是显示在appstore上面的
version 对应info.plist文件里的“Bundle versions string, short”字段,这才是真正的版本号,显示在appstore上的。由于之前学习的时候,上网查资料,有好多人老是把这两个给弄混了,我也纠结了一会,特此说明一下,以备记忆。
//获取appstore最新的版本号
NSString *newVersion = [rightDic objectForKey:@"version"];
//获取应用程序的地址
NSString *newURL = [rightDic objectForKey:@"trackViewUrl"];
//取得本地程序的版本号
NSDictionary *localDic =[[NSBundle mainBundle] infoDictionary];
NSString *localVersion =[localDic objectForKey:@"CFBundleShortVersionString"];
CCLog(@"appStore 版本号为:%@ 本地版本号为:%@",newVersion,localVersion);
- iOS 检测更新的实现
- iOS开发 检测版本更新的实现
- 【iOS】APP实现检测更新
- iOS简单实现版本更新检测
- iOS开发 APP如何实现检测更新
- iOS APP如何实现版本检测更新
- iOS开发 APP如何实现检测更新
- iOS APP如何实现版本检测更新
- iOS日常需求:iOS app实现检测更新功能
- iOS 检测版本更新
- iOS 检测版本更新
- IOS检测版本更新
- iOS检测版本更新
- iOS 检测版本更新
- iOS 检测版本更新
- IOS 版本更新检测
- iOS 检测版本更新
- iOS 检测版本更新
- TurtleWorld demo
- 进程的用户和用户组身份
- I/O流(四)—java如何添加到文件尾
- 在linux环境下编译运行OpenCV程序的两种方法
- C/C++语言面试题详细解答四
- iOS 检测更新的实现
- COCOS2DX-ANDROID之 APK签名,打包
- 使用PDFLib生成PDF文档,C代码实例--画图形
- Android编译服务器的管理之五:上电自启
- 框架,springmvc,mybatis,ehcache,源码
- bean作用域
- 2014 C专科C组第五题 套娃
- redis 使用服务器登录redis控制台,进行操作缓存
- 输入两个链表,找出它们的第一个公共结点。