App更新处理

为了良好的用户体验,App一般都会在启动的是时候去检查App版本号是否是最新的,不是最新的会弹框提示用户是否更新,虽然Appstore里面会出现提示,但是Appstore的提示不够明显,一般常用的做法有两种,第一种是在后台存一个版本号,然后每次App启动的时候去GET这个版本号,然后去跟项目里面的版本号作对比,第二种是直接获取Apple Appstore最新上线的版本号,两者都是通过对版本号作对比去提示用户,相差几乎不大,由于这里没有接口来模拟GET后台的版本号,这里就直接贴从Appstore获取的版本号代码,废话不多说,详情如下 :

// MARK: - 检查更新
- (void)checkUpdateInfo {
    __weak typeof(self)weakSelf = self;
    [[[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",kAppID]] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        AppDelegate *strongSelf = weakSelf;
        if (!error) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
            if ([dic[@"results"] count]) {
                NSDictionary *results = dic[@"results"][0];
                //App更新
                NSString *AppVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
                if ([results[@"version"] floatValue] > [AppVersion floatValue]) {
                    NSString *messageStr = [[results[@"releaseNotes"]  componentsSeparatedByString:@"。"]firstObject];
                    NSArray *titleArray = [messageStr componentsSeparatedByString:@"\n"];
                    NSMutableString *newStr = [NSMutableString new];
                    [newStr appendString:@" \n"];
                    for (NSString *subStr in titleArray) {
                        [newStr appendFormat:@"%@\n",subStr];
                    }
                    strongSelf->_messageStr = newStr;
                    //更新弹框
                    strongSelf->_alertVC = [UIAlertController alertControllerWithTitle:@"应用有新版本" message:newStr preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        //更新跳转
                        NSString *downloadURL =[NSString stringWithFormat:@"https://itunes.apple.com/us/app/%@/id%@?l=zh&ls=1&mt=8”,@“App名称的中文转码,可以在开发者itunes Connect中的我的App里面去查看”,kAppID];
                        NSURL *appStoreURL = [NSURL URLWithString:downloadURL];
                        if ([[UIApplication sharedApplication] canOpenURL:appStoreURL]) {
                            //跳转appstore
                            [[UIApplication sharedApplication] openURL:appStoreURL];
                        }
                    }];
        
                    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"下次" style:UIAlertActionStyleDefault handler:nil];
                    [strongSelf->_alertVC addAction:action1];
                    [strongSelf-> _alertVC addAction:action2];
                    [strongSelf runtimeProperty];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [[UIApplication sharedApplication].keyWindow.rootViewController  presentViewController:strongSelf->_alertVC animated:YES completion:nil];
                    });
                }
            }
        } else {
            NSLog(@"update Error = %@",error.localizedDescription);
        }
    }]resume];
}

基本思路就是这样了,如果对您有帮助,请star我一波!

你可能感兴趣的:(App更新处理)