美文网首页
iOS App 版本检测升级

iOS App 版本检测升级

作者: 丶天空蓝丶 | 来源:发表于2020-07-02 17:28 被阅读0次

    只需要知道自己的app ID,就可以使用版本检测升级功能。

    首先,根据app ID获取app在app store上的相关信息,然后拿到自己本地的版本号,和app store上获取的版本号比较,如果本地版本号较低,则提醒需要更新,展示app store上最新的版本号和最近的更新内容,最后,如果用户点击更新就跳转到app store上app对应界面进行更新。(另,因为当时要封装成静态库,网络请求用的是苹果自带的,可以换成afn)

    根据app ID获取app store上app的信息
    +(void)checkVersionUpdate:(NSString *)appId {
        //定义的app的地址
        //country 上架国家(全部国家可不写) id appId。
        NSString *urld = [NSString stringWithFormat:@"https://itunes.apple.com/lookup?country=cn&id=%@", appId];
        NSURL *url = [NSURL URLWithString:urld];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
        [request setHTTPMethod:@"GET"];
        
        NSURLSession *session = [NSURLSession sharedSession];
        
        NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            
            NSMutableDictionary *receiveStatusDic = [[NSMutableDictionary alloc] init];
            if (data) {
                NSDictionary *receiveDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves|NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:nil];
                NSArray *array =receiveDic[@"results"];
                if (array.count == 0) {
                    return ;
                }
                //获取appstore最新版本号
                NSString *version = [array[0] objectForKey:@"version"];
                //获取更新信息
                NSString *note = [array[0] objectForKey:@"releaseNotes"];
                
                if ([version intValue]>0) {
                    [receiveStatusDic setValue:@"-1" forKey:@"status"];
                    [receiveStatusDic setValue:version forKey:@"version"];
                    [receiveStatusDic setValue:note forKey:@"note"];
                    [receiveStatusDic setValue:appId forKey:@"appId"];
                    //请求的有数据,进行版本比较
                    [self performSelectorOnMainThread:@selector(receiveData:) withObject:receiveStatusDic waitUntilDone:YES];
                } else {
                    [receiveStatusDic setValue:@"-1" forKey:@"status"];
                }
            } else {
                [receiveStatusDic setValue:@"-1" forKey:@"status"];
            }
            
            
        }];
        
        [task resume];
    }
    
    app store获取的版本号和本地版本号比较
    + (void)receiveData:(id)sender {
        //获取APP自身版本号
        NSString *localVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"];
        
        NSArray *localArray = [localVersion componentsSeparatedByString:@"."];
        NSArray *versionArray = [sender[@"version"] componentsSeparatedByString:@"."];
        
        //比较
        NSUInteger count = versionArray.count;
        if (versionArray.count > localArray.count) {
            count = localArray.count;
        }
        for (int i = 0; i < count; i++) {
            if ([localArray[i] intValue] < [versionArray[i] intValue]) {
                [self alertUpdateVersion:sender];
                return;
            }
            //审核时本地大于线上,开发时也有可能
            if ([localArray[i] intValue] > [versionArray[i] intValue]) {
                NSLog(@"本地版本大于线上");
                return;
            }
        }
        //出现了1.2和1.2.3的情况,肯定是长的那个是高版本,因为不存在1.2和1.2.0对比的情况
        if (versionArray.count > localArray.count) {
            [self alertUpdateVersion:sender];
            return;
        }
        
        NSLog(@"目前没有新版本");
    }
    
    更新提示
    + (void)alertUpdateVersion:(NSMutableDictionary *)dic{
        [dic setValue:@"1" forKey:@"status"];
        
        NSString *alertStr = [NSString stringWithFormat:@"检测到较新版本%@。更新内容:\n%@\n\n是否需要进行更新?",dic[@"version"],dic[@"note"]];
        
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"版本检测" message:alertStr preferredStyle:(UIAlertControllerStyleAlert)];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"是的" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            [self goToUpdate:dic[@"appId"]];
        }];
        [alertVC addAction:action];
        
        if ([dic[@"updateFlag"] integerValue] == UpdateOptional){
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"不,谢谢" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {}];
            [alertVC addAction:cancelAction];
        }
        
        [[self getCurrentViewController] presentViewController:alertVC animated:false completion:nil];
    }
    
    获取当前视图的活动页面
    + (UIViewController *)getCurrentViewController {
        UIViewController *root = [UIApplication sharedApplication].keyWindow.rootViewController;
        UIViewController *currentVC = [self GetNextVC:root];
        
        return currentVC;
    }
    
    //使用的递归方法
    + (UIViewController *)GetNextVC:(UIViewController *)rootVC
    
    {
        UIViewController *currentVC;
        if ([rootVC presentedViewController]) {
            rootVC = [rootVC presentedViewController];
        }
        
        //    UITabBarController
        if ([rootVC isKindOfClass:[UITabBarController class]]) {
            currentVC = [self GetNextVC:[(UITabBarController *)rootVC selectedViewController]];
        }
        //    UINavigationController
        else if ([rootVC isKindOfClass:[UINavigationController class]]){
            currentVC = [self GetNextVC:[(UINavigationController *)rootVC visibleViewController]];
        }
        //    普通的ViewController
        else {
            currentVC = rootVC;
        }
        return currentVC;
        
    }
    
    跳转到app store更新
    + (void)goToUpdate:(NSString *)appId {
        //跳转地址链接
        NSString *str = [NSString stringWithFormat: @"itms-apps://itunes.apple.com/cn/app/id%@?mt=8", appId];
        UIApplication *app = [UIApplication sharedApplication];
        NSURL *url = [NSURL URLWithString:str];
        if ([app canOpenURL:url]) {
            if (@available(iOS 10.0,*)) {
                [app openURL:url options:@{} completionHandler:nil];
            } else {
                [app openURL:url];
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS App 版本检测升级

          本文链接:https://www.haomeiwen.com/subject/ssirqktx.html