使用这个方法,只传入你的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"];
NSInteger appStoreVersionNumber = [appStoreVersion integerValue];
NSInteger localVersionNumber = [localVersion integerValue];
if (appStoreVersionNumber<=localVersionNumber) {
fail(0,@"暂无更新");
}else{
NSDictionary *updateInfo = @{@"title":@"应用更新", //标题
@"version":appStoreVersion, //版本
@"notes":[appStoreInfo objectForKey:@"releaseNotes"], //版本更新描述
@"url":[appStoreInfo objectForKey:@"trackViewUrl"], //app页url
};
success(updateInfo);
}
}
}];
[task resume];
}
ps:
1.只要在itunes connect里创建了应用就可以看到appId了,无需等审核完成
2.此方法检查的是version,并非build,在上架的时候控制好这个。
3.请求返回app的众多信息,需要的自己拿来用。