zoukankan      html  css  js  c++  java
  • ios开发中如何实现软件版本更新


    苹果给了我们一个接口,能根据应用id请求一些关于应用的信息。我们可以根据返回的信息,来判断版本是否和应用的版本一致,如果不一致,那么就出现新的版本了。这时,就需要向用户提醒有新的版本,需要更新。具体步骤如下:

    1 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    2 [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",appleID]]];
    3 [request setHTTPMethod:@"GET"];
    4 NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    5 NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil];

    这里,我们通过同步请求,解析json数据,得到了数据。
    好的,我们这里需要,version,trackViewUrl,trackName。
    1 NSString *latestVersion = [releaseInfo objectForKey:@"version"];
    2 NSString *trackViewUrl1 = [releaseInfo objectForKey:@"trackViewUrl"];//地址trackViewUrl
    3 NSString *trackName = [releaseInfo objectForKey:@"trackName"];//trackName

    获取此应用的版本号
    1 NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"];

    通过latestVersion和currentVersion的比较,来判断是否有新的更新。
    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
            NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"];
            double doubleCurrentVersion = [currentVersion doubleValue];
            
            if (doubleCurrentVersion < doubleUpdateVersion) {
                
                UIAlertView *alert;
                alert = [[UIAlertView alloc] initWithTitle:trackName
                                                   message:@"有新版本,是否升级!"
                                                  delegate: self
                                         cancelButtonTitle:@"取消"
                                         otherButtonTitles: @"升级", nil];
                alert.tag = 1001;
                [alert show];
            }
            else{
                UIAlertView *alert;
                alert = [[UIAlertView alloc] initWithTitle:trackName
                                                   message:@"暂无新版本"
                                                  delegate: nil
                                         cancelButtonTitle:@"好的"
                                         otherButtonTitles: nil, nil];
                [alert show];
            }

    如果有新的版本,那么就跳转至下载页面,这里就用到了trackViewUrl,trackViewUrl是全路径,直接请求。
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:trackViewUrl]];

    好的,这就是版本更新的全部步骤
     
    方法二:

    如果我们要检测app版本的更新,那么我们必须获取当前运行app版本的版本信息和appstore 上发布的最新版本的信息。

    当前运行版本信息可以通过info.plist文件中的bundle version中获取:

    [cpp] view plaincopy
     
     
    1. NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];  
    2.     CFShow(infoDic);  
    3.       
    4.     NSString *appVersion = [infoDic objectForKey:@"CFBundleVersion"];  

    这样就获取到当前运行的app的版本了

    要获取当前app store上的最新的版本,有两种方法,

    一、在某特定的服务器上,发布和存储app最新的版本信息,需要的时候向该服务器请求查询。

    二、从app store上查询,可以获取到app的作者,连接,版本等。官方相关文档

    www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.htm

    具体步骤如下:
    1,用 POST 方式发送请求:
    http://itunes.apple.com/search?term=你的应用程序名称&entity=software

    更加精准的做法是根据 app 的 id 来查找:
    http://itunes.apple.com/lookup?id=你的应用程序的ID

    #define APP_URL http://itunes.apple.com/lookup?id=你的应用程序的ID

    你的应用程序的ID 是 itunes connect里的 Apple ID

    2,从获得的 response 数据中解析需要的数据。因为从 appstore 查询得到的信息是 JSON 格式的,所以需要经过解析。解析之后得到的原始数据就是如下这个样子的:
    {  
        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; 
          }  
        );  
    }  

    然后从中取得 results 数组即可,具体代码如下所示:

    NSDictionary *jsonData = [dataPayload JSONValue];  
    NSArray *infoArray = [jsonData objectForKey:@"results"];  
    NSDictionary *releaseInfo = [infoArray objectAtIndex:0];  
    NSString *latestVersion = [releaseInfo objectForKey:@"version"];  
    NSString *trackViewUrl = [releaseInfo objectForKey:@"trackViewUrl"];  

    如果你拷贝 trackViewUrl 的实际地址,然后在浏览器中打开,就会打开你的应用程序在 appstore 中的介绍页面。当然我们也可以在代码中调用 safari 来打开它。
    UIApplication *application = [UIApplication sharedApplication];  
    [application openURL:[NSURL URLWithString:trackViewUrl]];  

    代码如下:

    -(void)onCheckVersion

    {

        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];

        //CFShow((__bridge CFTypeRef)(infoDic));

        NSString *currentVersion = [infoDic objectForKey:@"CFBundleVersion"];

        NSString *URL = @"http://itunes.apple.com/lookup?id=你的应用程序的ID";

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

        [request setURL:[NSURL URLWithString:URL]];

        [request setHTTPMethod:@"POST"];

        NSHTTPURLResponse *urlResponse = nil;

        NSError *error = nil;

        NSData *recervedData = [NSURLConnection sendSynchronousRequest:requestreturningResponse:&urlResponse error:&error];

        

        NSString *results = [[NSString alloc] initWithBytes:[recervedData bytes] length:[recervedDatalength] encoding:NSUTF8StringEncoding];

        NSDictionary *dic = [results JSONValue];//  字符串转json对象(分类方法,详细介绍请看我另外一篇笔记)

        NSArray *infoArray = [dic objectForKey:@"results"];

        if ([infoArray count]) {

            NSDictionary *releaseInfo = [infoArray objectAtIndex:0];

            NSString *lastVersion = [releaseInfo objectForKey:@"version"];

            

            if (![lastVersion isEqualToString:currentVersion]) {

                //trackViewURL = [releaseInfo objectForKey:@"trackVireUrl"];

                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"有新的版本更新,是否前往更新?" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"更新", nil];

                alert.tag = 10000;

                [alert show];

            }

            else

            {

                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"此版本为最新版本" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

                alert.tag = 10001;

                [alert show];

            }

        }

    }

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

    {

        if (alertView.tag==10000) {

            if (buttonIndex==1) {

                NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com"];

                [[UIApplication sharedApplication]openURL:url];

            }

        }

    }

     
  • 相关阅读:
    算法竞赛入门经典习题2-3 韩信点兵
    ios入门之c语言篇——基本函数——5——素数判断
    ios入门之c语言篇——基本函数——4——数值交换函数
    144. Binary Tree Preorder Traversal
    143. Reorder List
    142. Linked List Cycle II
    139. Word Break
    138. Copy List with Random Pointer
    137. Single Number II
    135. Candy
  • 原文地址:https://www.cnblogs.com/yjg2014/p/3792944.html
Copyright © 2011-2022 走看看