使用这个方法,只传入你的appId即可,在只有需要更新时才做处理。
不依赖三方,不依赖后台
/* 随便可修改的block */typedef void(^completionHandlerSuccess)(NSDictionary *responseObject);typedef void(^completionHandlerFail)(int code,NSString *message);
/** 根据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];}