搜索
您的当前位置:首页正文

iOS APP检查版本更新

来源:二三娱乐

在开发中,我们可能会遇到这样的需求,当AppStore中有新版本需要更新迭代,用户在点开APP的时候弹出提示框提醒用户去AppStore更新。
这里需要我们判断当前APP与AppStore中的版本差别,如果一样,不需要提示;如果不一样就弹出提示框,给用户提示更新APP版本。

-(void)viewDidLoad{
    [super viewDidLoad];
    //iOS应用版本检测
    [self versionDetection];
}
-(void)versionDetection{
    //获取当前发布的版本的Version
    NSString *currentVersionStr = [NSString stringWithContentsOfURL:[NSURL  encoding:NSUTF8StringEncoding error:nil];
    //判断字符串是否为nil,长度是不是大于0,以及是不是取得版本
    if (currentVersionStr != nil && [currentVersionStr length] > 0 && [currentVersionStr rangeOfString:@"version"].length == 7) {
        [self checkAppUpdate:currentVersionStr];
    }
}
图1.png
-(void)checkAppUpdate:(NSString *)currentVersionStr{
    //string转dictionary
    NSDictionary *versionDict = [global dictionaryWithJSonString:currentVersionStr];
    //获取当前版本
    NSString *versionStr = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
    //线上App版本
    NSString *appVersion = [currentVersionStr substringFromIndex:[currentVersionStr rangeOfString:@"\"version\":"].location+10];
    appVersion = [[appVersion substringToIndex:[appVersion rangeOfString:@","].location] stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    NSArray *localArray = [versionStr componentsSeparatedByString:@"."];
    NSArray *versionArray = [appVersion componentsSeparatedByString:@"."];
    //这里比较版本号不是不一样就提示更新升级。而是当前版本号如果比AppStore版备号小的时候提示弹框升级。这样做的最大好处就是苹果在审核App时不会出现提示升级。
    if ((versionArray.count == 3) && (localArray.count == versionArray.count)) {
        if ([localArray[0] intValue] <  [versionArray[0] intValue]) {
            [self uodateVersion:currentVersionStr];
        }else if ([localArray[0] intValue]  ==  [versionArray[0] intValue]){
            if ([localArray[1] intValue] <  [versionArray[1] intValue]) {
               [self uodateVersion:currentVersionStr];
            }else if ([localArray[1] intValue] ==  [versionArray[1] intValue]){
                if ([localArray[2] intValue] <  [versionArray[2] intValue]) {
                    [self uodateVersion:currentVersionStr];
                }
            }
        }
    }
-(void)uodateVersion:(NSString *)currentVersion {
        NSDictionary *versionDict = [global dictionaryWithJSonString:currentVersion];
        NSString * releaseNotesString= [[[versionDict objectForKey:@"results"] objectAtIndex:0] valueForKey:@"releaseNotes"];
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"发现新版本" message:releaseNotesString preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSString *url = 
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
        }];
        [alertController addAction:cancelAction];
        [alertController addAction:okAction];
        [self presentViewController:alertController animated:YES completion:nil];
}

纠正,现在这种方式会被苹果给拒绝,可以换成后台控制这个弹出框的显示
结语:如有错误请留言!
如果疑问请留言,一起进步

Top