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:[[NSDictionaryallocinitWithObjectsAndKeys: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 allocinitWithTitle:@"提示" 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,并跳转到应用下载页面

        [[UIApplicationsharedApplicationopenURL:[NSURLURLWithString:updateURL]];

 

    } else if (buttonIndex == 2) {

        //去itunes中更新

        [[UIApplicationsharedApplicationopenURL:[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);

0 0
原创粉丝点击