iOSApp內(nèi)提示用戶更新版本

完成這個需求大體四步
1.獲取App Store項目版本信息
2.獲取本地版本信息
3.本地與線上版本進行對比
4.跳轉(zhuǎn)App Store下載新版本

NSString *sign = [[NSUserDefaults standardUserDefaults] valueForKey:@"downLoadSign"];
NSUInteger num = [sign integerValue];
NSString *securlStr = @"https://itunes.apple.com//lookup?id=1317191959";
NSURL *url = [NSURL URLWithString:securlStr];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[NSURLConnection connectionWithRequest:req delegate:self];

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
NSError *error;
//解析
NSDictionary *appInfo = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSArray *infoContent = [appInfo objectForKey:@"results"];
//最新版本號
NSString *version = [[infoContent objectAtIndex:0] objectForKey:@"version"];
NSString *secVersion = [version stringByReplacingOccurrencesOfString:@"." withString:@""];
NSInteger versionNum = [secVersion integerValue];
NSLog(@"最新版本號%@",version);
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
//當前版本號
NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
NSString *secCurrentVersion = [currentVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
NSInteger secVersionNum = [secCurrentVersion integerValue];
if (secVersionNum < versionNum) {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"AppStore上有新版本可以下載" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"去下載" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSString *urlStr = @"itms-apps://itunes.apple.com/us/app/hong-xing-hui-yao/id1317191959?l=zh&ls=1&mt=8";
        NSDictionary *dic = [NSDictionary dictionary];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]  options:dic completionHandler:^(BOOL success) {
        }];
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"暫不考慮" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:sureAction];
    [alert addAction:cancel];
    UIWindow *aW = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    aW.rootViewController = [[UIViewController alloc]init];
    aW.windowLevel = UIWindowLevelAlert + 1;
    [aW makeKeyAndVisible];
    [aW.rootViewController presentViewController:alert animated:YES completion:nil];
  }
}

至于優(yōu)化完善工作 就請讀者根據(jù)自身項目需求來完善啦

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容