1. 程式人生 > >iOS APP內彈窗推送版本更新資訊(實現跳轉、強制更新等)

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

1、一開啟APP就檢測版本更新資訊,則需要AppDelegate.mm裡面新增:

    NSString *version = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleVersion"];
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    [mgr.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil]];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"id"] = @"123456789";// 你程式的apple ID號
    [mgr POST:@"http://itunes.apple.com/cn/lookup?id=123456789" parameters:dict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        // App_URL http://itunes.apple.com/lookup
        NSArray *array = responseObject[@"results"];
        if (array.count != 0) {// 先判斷返回的資料是否為空   沒上架的時候是空的
            NSDictionary *dict = array[0];
 
            if ([dict[@"version"] floatValue] > [subVersion floatValue]) {
                //如果有新版本 這裡要注意下如果你版本號寫得是1.1.1或者1.1.1.1這樣的格式,就不能直接轉floatValue,自己想辦法比較判斷。
                UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
                alertWindow.rootViewController = [[UIViewController alloc] init];
                alertWindow.windowLevel = UIWindowLevelAlert + 1;
                [alertWindow makeKeyAndVisible];
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"更新提示" message:@"發現新版本。為保證各項功能正常使用,請您儘快更新。" preferredStyle:UIAlertControllerStyleAlert];
                //顯示彈出框
                [alertWindow.rootViewController presentViewController:alert animated:YES completion:nil];
                
                [alert addAction:[UIAlertAction actionWithTitle:@"現在更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/id123456789?mt=8"]];
                    //這裡寫的URL地址是該app在app store裡面的下載連結地址,其中ID是該app在app store對應的唯一的ID編號。
                    NSLog(@"點選現在升級按鈕,跳轉");
                }]];
                
                [alert addAction:[UIAlertAction actionWithTitle:@"下次再說" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    NSLog(@"點選下次再說按鈕");  //如果不add這段Action,則彈窗中只有1個按鈕,即強制使用者更新
                }]];
                
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
    return YES;

2、如果要每次開啟某個頁面就彈出,可將檢查更新程式碼放在該頁面的viewController內。例如有個頁面中功能不允許為登入使用者操作,則在該viewController中嵌入檢查更新程式碼。

以上程式碼親測可用,已經使用了好幾個版本啦。有些說法是內建版本推送更新會被拒,其實並不會,只要在稽核期間保證不會彈出更新視窗就好了。