iOS APP内弹窗推送版本更新信息(实现跳转、强制更新等)

项目必须要做这个,自己顺便记录一下

#pragma mark - 判断是否需要提示更新App
- (void)updateVersion:(NSString *)path {
    // 获取本地版本号
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    // 取得AppStore信息
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil]];
    
    [manager GET:path parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"responseObject --- %@", responseObject);
        
        NSData *dataCast = responseObject;
        NSString *dataString = [[NSString alloc] initWithData:dataCast encoding:NSUTF8StringEncoding];
        NSData *data =[dataString dataUsingEncoding:NSUTF8StringEncoding];
        id json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSArray *array = json[@"results"];
        
        // app store 最新版本号
        NSString *newVersion;
        for (NSDictionary *dic in array) {
            newVersion = [dic valueForKey:@"version"];
        }
        
        // 取更新日志信息
        NSString *changeStr;
        for (NSDictionary *dic in array) {
            changeStr = [dic valueForKey:@"releaseNotes"];
        }
        
        // app store 跳转版本链接
        NSString *trackViewUrl;
        for (NSDictionary *dic in array) {
            trackViewUrl = [dic valueForKey:@"trackViewUrl"];
        }
        
        NSLog(@"app store 的更新信息 --- %@, app store 的最新版本号 --- %@, 跳转版本链接 --- %@", changeStr, currentVersion, trackViewUrl);
        
        NSInteger index = [self versionCompareFirst:newVersion andVersionSecond:currentVersion];
        
        // AppStore版本号大于当前版本号,强制更新
        if ([self versionCompareFirst:newVersion andVersionSecond:currentVersion] == 0) {
            // 弹窗 更新
            NSLog(@"");
            [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
            UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"发现新版本" message:[NSString stringWithFormat:@"新版本%@急需更新",newVersion] preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *sureAction=[UIAlertAction actionWithTitle:@"去更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                NSLog(@"");
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:trackViewUrl]];
            }];
            [alertController addAction:sureAction];
            
            UIWindow   *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
            alertWindow.rootViewController = [[UIViewController alloc] init];
            alertWindow.windowLevel = UIWindowLevelAlert + 1;
            [alertWindow makeKeyAndVisible];
            [alertWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
            
        } else {
            // 正常进入程序
            [self loadRootVC];
        }
        
        NSLog(@"");
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 网络异常时,正常进入程序
        NSLog(@"");
        [self loadRootVC];
    }];
}
复制代码

path为AppStore链接:http://itunes.apple.com/cn/lookup?id=(你的Apple ID)

啊啊啊以后不要再说为什么安卓可以实现,而iOS不可以

代码质量有点渣~大家见谅


转载于:https://juejin.im/post/5c3562a0f265da614d095c0d

你可能感兴趣的:(iOS APP内弹窗推送版本更新信息(实现跳转、强制更新等))