使用這個方法,只傳入你的appId即可,在只有需要更新時才做處理。
不依賴三方,不依賴后臺
/* 隨便可修改的block */typedef void(^completionHandlerSuccess)(NSDictionary *responseObject);typedef void(^completionHandlerFail)(int code,NSString *message);
/** 根據(jù)appid獲取appstore上app的版本和當前app的版本進行對比贰健,檢查更新 @param appId app上架后的id @param success @param fail */- (void)checkAppVersionWithAppId:(NSString *)appId Success:(completionHandlerSuccess)success andFail:(completionHandlerFail)fail{ NSString *url = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",appId]; NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:20]; [request setHTTPMethod:@"GET"];//method NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error]; NSLog(@"apiJsonResult = %@",result); id responseObject = result; if (responseObject != nil && [responseObject isKindOfClass:[NSDictionary class]] && [[responseObject objectForKey:@"results"] isKindOfClass:[NSArray class]] && [[responseObject objectForKey:@"results"] firstObject] !=nil && [[[responseObject objectForKey:@"results"] firstObject] isKindOfClass:[NSDictionary class]]) { //返回的json信息 NSDictionary *appStoreInfo = [[responseObject objectForKey:@"results"] firstObject]; //當前app信息 NSDictionary *localInfo = [[NSBundle mainBundle] infoDictionary]; //app在appstrore上版本 NSString *appStoreVersion = [appStoreInfo objectForKey:@"version"]; //app當前版本 NSString *localVersion = [localInfo objectForKey:@"CFBundleShortVersionString"]; if ([appStoreVersion isEqualToString:localVersion]) { fail(0,@"暫無更新"); }else{ NSDictionary *updateInfo = @{@"title":@"應用更新", //標題 @"version":appStoreVersion, //版本 @"notes":[appStoreInfo objectForKey:@"releaseNotes"], //版本更新描述 @"url":[appStoreInfo objectForKey:@"trackViewUrl"], //app頁url }; success(updateInfo); } } }]; [task resume];}