使用這個方法,只傳入你的appId即可,在只有需要更新時才做處理。
不依賴三方翼雀,不依賴后臺
/* 隨便可修改的block */
typedef void(^completionHandlerSuccess)(NSDictionary *responseObject);
typedef void(^completionHandlerFail)(int code,NSString *message);
/**
根據(jù)appid獲取appstore上app的版本和當(dāng)前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];
//當(dāng)前app信息
NSDictionary *localInfo = [[NSBundle mainBundle] infoDictionary];
//app在appstrore上版本
NSString *appStoreVersion = [appStoreInfo objectForKey:@"version"];
//app當(dāng)前版本
NSString *localVersion = [localInfo objectForKey:@"CFBundleShortVersionString"];
NSInteger appStoreVersionNumber = [appStoreVersion integerValue];
NSInteger localVersionNumber = [localVersion integerValue];
if (appStoreVersionNumber<=localVersionNumber) {
fail(0,@"暫無更新");
}else{
NSDictionary *updateInfo = @{@"title":@"應(yīng)用更新", //標(biāo)題
@"version":appStoreVersion, //版本
@"notes":[appStoreInfo objectForKey:@"releaseNotes"], //版本更新描述
@"url":[appStoreInfo objectForKey:@"trackViewUrl"], //app頁url
};
success(updateInfo);
}
}
}];
[task resume];
}
ps:
1.只要在itunes connect里創(chuàng)建了應(yīng)用就可以看到appId了,無需等審核完成
2.此方法檢查的是version孩擂,并非build狼渊,在上架的時候控制好這個。
3.請求返回app的眾多信息类垦,需要的自己拿來用狈邑。
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者