iOS APP內彈窗推送版本更新信息(實現跳轉、強制更新等)

項目必需要作這個,本身順便記錄一下javascript

#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)html

啊啊啊之後不要再說爲何安卓能夠實現,而iOS不能夠java

代碼質量有點渣~你們見諒
json

相關文章
相關標籤/搜索