iOS开发 APP如何实现检测更新

来源:互联网 发布:coach 知乎 编辑:程序博客网 时间:2024/06/06 03:07

App检测更新可以使用两种方法。

第一种是和安卓等系统一样,获取自己服务器的App版本号与已安装的App版本号比较;

第二种是根据已发布到App Store上的应用版本号与已安装的App版本号比较更新。

两种方法比较

第一种检测更新方法的优点是:检测更新速度快、检测稳定;缺点是:和App Store上的应用版本号不同步(App上架需要审核时间,不确定什么时候成功更新到App Store上)。

第二种方法检测更新方法的优点是:检测版本号是实时同步的;缺点是:苹果网络不稳定,检测更新有点延时,部分App获取不到任何参数。代码在github的cjq002的CheckVersion上。

大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑大笑

版本号比较方法

      1、获取App当前版本号;

      2、使用NSString自带方法进行比较。


跳转到App Store下载

      1、格式化下载链接;

      2、使用系统自带方法跳转到App Store应用下载页。


方法一:获取自己服务器版本号检查

      1、通过网络请求获取服务器上的版本号;

      2、调用上面的比较方法,比较前应用版本号和服务器上的版本号;

      3、如果有版本更新则跳转到App Store上下载。

注:获取服务器版本号就需要自己去请求了。

方法二:获取App Store上架版本号检查

      1、通过网络同步请求获取App Store上对应APP ID的应用信息;

      2、提取信息上的最新版本号等信息;

      3、提取最新版本号;

      4、调用上面的比较方法,比较前应用版本号和最新版本号;

      5、如果有版本更新则跳转到App Store上下载。



运行效果(以第二种方法,iOS版企鹅应用为例)

      当前版本为3.2.1,请求控制台返回:“发现新版本 6.5.6”

(Demo在真机上会跳转到AppStore的企鹅下载页);

      当前版本为6.5.6,请求控制台返回:“没有新版本”;

      当前版本为6.6.6,请求控制台返回:“没有新版本”。




以上是全部步骤, 为了方便大家使用,下面粘上代码。

- (BOOL)compareVersion:(NSString *)serverVersion {

    // 获取当前版本号

    NSDictionary *infoDic = [[NSBundlemainBundle] infoDictionary];

    NSString *appVersion = [infoDicobjectForKey:@"CFBundleShortVersionString"];

    

    // MARK: 比较当前版本和新版本号大小

    /*

     typedef enum _NSComparisonResult {

        NSOrderedAscending = -1L,   升序

        NSOrderedSame,              等于

        NSOrderedDescending         降序

     }

     */

    

    // MARK比较方法

    if ([appVersioncompare:serverVersion options:NSNumericSearch] ==NSOrderedAscending) {

        NSLog(@"发现新版本 %@", serverVersion);

        returnYES;

    }else {

        NSLog(@"没有新版本");

        returnNO;

    }

    

}


- (void)aaa {

    

    // 下载地址可以是trackViewUrl,也可以是items-apps://itunes.apple.com/app/idxxxxxxxxxx

    NSString *appId =@"xxxxxxxxx";

    NSString *string = [NSStringstringWithFormat:@"items-apps://itunes.apple.com/app/id%@", appId];

    [[UIApplicationsharedApplication] openURL:[NSURLURLWithString:string]];

}


- (BOOL)checkAppStoreVersionWithAppId:(NSString *)appId {

    

    // MARK: 拼接链接,转换成URL

    NSString *checkUrlString = [NSStringstringWithFormat:@"https://itunes.apple.com/lookup?id=%@", appId];

    NSURL *checkUrl = [NSURLURLWithString:checkUrlString];

    

    // MARK: 获取网路数据AppStoreapp的信息

    NSString *appInfoString = [NSStringstringWithContentsOfURL:checkUrl encoding:NSUTF8StringEncodingerror:nil];

    

    // MARK: 字符串转json转字典

    NSError *error =nil;

    NSData *JSONData = [appInfoStringdataUsingEncoding:NSUTF8StringEncoding];

    NSDictionary *appInfo = [NSJSONSerializationJSONObjectWithData:JSONData options:NSJSONReadingMutableLeaveserror:&error];

    

    if (!error && appInfo) {

        NSArray *resultArr = appInfo[@"results"];

        NSDictionary *resultDic = resultArr.firstObject;

        

        // 版本号

        NSString *version = resultDic[@"trackName"];

        

        // 下载地址

        NSString *trackViewUrl = resultDic[@"trackViewUrl"];

        

        // FRXME:比较版本号

        return [selfcompareVersion:version];

        

    }else {

        // 返回错误想当于没有更新吧

        returnNO;

    }

    

}


- (void)viewDidLoad {

    

    [super viewDidLoad];

    

    staticNSString *appId = @"xxxxxx";

    

    // 返回是否有新版本

    BOOL update = [selfcheckAppStoreVersionWithAppId:appId];

    

    // 添加自己的代码可以弹出一个提示框 这里不实现了

    if (update) {

        // 下载地址可以是trackViewUrl,也可以是item-apps://itunes.apple.com/app/idxxxxxxxx

        NSString *string = [NSStringstringWithFormat:@"items-apps://itunes.apple.com/app/idxxxxx"];

        [[UIApplicationsharedApplication] openURL:[NSURLURLWithString:string]];

    }

}



1 0