NSString * strurl = [[NSString alloc] initWithFormat:@"http://itunes.apple.com/lookup?id=%@",AppIDs];//替換為自己App的ID NSURLSession *session=[NSURLSession sharedSession]; NSURL *url = [NSURL URLWithString:strurl]; //3.創建可變的請求對象 NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url]; NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error){ //8.解析數據 NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; NSArray * results = dict[@"results"]; NSDictionary * dic = results.firstObject; NSString * lineVersion = dic[@"version"];//版本號 NSString * releaseNotes = dic[@"releaseNotes"];//更新說明 NSString * trackViewUrl = dic[@"trackViewUrl"];//鏈接 NSLog(@"App store版本號:%@",lineVersion); NSLog(@"更新說明:%@",releaseNotes); NSLog(@"App下載鏈接:%@",trackViewUrl); NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; //獲取字符串中的數字 self.applocalversion = [[self.applocalversion componentsSeparatedByCharactersInSet:nonDigitCharacterSet] componentsJoinedByString:@""];//本地的app版本 lineVersion = [[lineVersion componentsSeparatedByCharactersInSet:nonDigitCharacterSet] componentsJoinedByString:@""];//App store的app版本 NSLog(@"結果是本地的app版本:%@ \n App store的app版本:%@ ",self.applocalversion,lineVersion); // 獲取NSUserDefaults對象 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:lineVersion forKey:@"App_store_version"]; [defaults synchronize]; //如果要立刻保存就需要這行代碼 if ([lineVersion integerValue]>[self.applocalversion integerValue]) {//App store的app版本>//本地的app版本 時要提醒升級 dispatch_async(dispatch_get_main_queue(), ^{ self.nav.tabBarItem.badgeValue = @"new"; }); }else{ NSLog(@"111沒有新版本不用更新提示"); } }]; //7.執行任務 [dataTask resume];
