iOS常用

ios 检测更新

2020-06-22  本文已影响0人  肉肉要次肉

Git地址:https://github.com/wolfhous/HSUpdateApp

1、将这文件拖入你的工程中,在appdelegate中调用检测

2、引入头文件 #import "HSUpdateApp.h"

3、

[HSUpdateApp hs_updateWithAPPID:@"1492599319" withBundleId:nil block:^(NSString *currentVersion, NSString *storeVersion, NSString *openUrl, BOOL isUpdate) {

        SLog(@"appdelegate: 本地:%@,商店:%@",currentVersion,storeVersion);

            if (isUpdate) {

                [self showAlertViewTitle:@"有更新" subTitle:[NSString stringWithFormat:@"检测到新版本%@,是否更新?",storeVersion] openUrl:openUrl];

           }else{

                NSLog(@"当前版本%@,商店版本%@,不需要更新",currentVersion,storeVersion);

            }

 }];

-(void)showAlertViewTitle:(NSString*)titlesubTitle:(NSString*)subTitleopenUrl:(NSString*)openUrl{

    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:subTitle preferredStyle:UIAlertControllerStyleAlert];

    NSLog(@"hhh:%@",openUrl);

    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        if([[UIDevice currentDevice].systemVersion floatValue] >= 10.0){

            if([[UIApplicationsharedApplication]respondsToSelector:@selector(openURL:options:completionHandler:)]) {

                if(@available(iOS10.0, *)) {

                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:openUrl] options:@{} completionHandler:^(BOOL success) {

                    }];

                }else{

                    // Fallback on earlier versions

                }

            }else{

                BOOLsuccess = [[UIApplicationsharedApplication]openURL:[NSURLURLWithString:openUrl]];

                NSLog(@"Open  %d",success);

            }

        }else{

            bool can = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:openUrl]];

            if(can){

                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:openUrl]];

            }

        }

    }];

    [alertVCaddAction:sure];

    [self.window.rootViewController presentViewController:alertVC animated:YES completion:nil];

}

以上就是通过从appstore中获取到最新的app版本号跟用户本地的版本号去做对比,以此来判断是否弹窗提示用户去跳转到appstore进行更新。

但是有一个缺点就是appstore更新的进度往往会有延迟,相同的app,用户手机的appstore不一定同一时间更新出最新的版本号。

如果有希望可以第一时间提示用户更新的话,就通过自己服务器来返回版本号会更及时。

上一篇 下一篇

猜你喜欢

热点阅读