ios 關於如何在app裡面設定版本更新功能
阿新 • • 發佈:2020-09-17
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"版本檢測更新"; self.view.backgroundColor = [UIColor whiteColor]; [self checkVersion]; //檢測升級 } -(void)checkVersion { NSString *newVersion; NSURL*url = [NSURL URLWithString:@"http://itunes.apple.com/cn/lookup?id=1139094792"];//這個URL地址是該app在iTunes connect裡面的相關配置資訊。其中id是該app在app store唯一的ID編號。 NSString *jsonResponseString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; NSLog(@"通過appStore獲取的資料資訊:%@",jsonResponseString); NSData*data = [jsonResponseString dataUsingEncoding:NSUTF8StringEncoding]; // 解析json資料 id json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSArray *array = json[@"results"]; for (NSDictionary *dic in array) { newVersion = [dic valueForKey:@"version"]; } NSLog(@"通過appStore獲取的版本號是:%@",newVersion); //獲取本地軟體的版本號 NSString *localVersion = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleVersion"]; NSString *msg = [NSString stringWithFormat:@"你當前的版本是V%@,發現新版本V%@,是否下載新版本?",localVersion,newVersion]; //對比發現的新版本和本地的版本 if ([newVersion floatValue] > [localVersion floatValue]) { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"升級提示"message:msg preferredStyle:UIAlertControllerStyleAlert]; [self 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/yi-ka-tongbic-ban/id1139094792?l=en&mt=8"]];這裡寫的URL地址是該app在app store裡面的下載連結地址,其中ID是該app在app store對應的唯一的ID編號。 NSLog(@"點選現在升級按鈕"); }]]; [alert addAction:[UIAlertAction actionWithTitle:@"下次再說" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"點選下次再說按鈕"); }]]; } }