iOS 版本更新
typedef void(^UpdateBlock)(NSString *currentVersion,NSString *storeVersion, NSString *openUrl,BOOL isUpdate);
-(void)updateWithAPPID:(NSString *)appId withBundleId:(NSString *)bundelId block:(UpdateBlock)block;
-(void)updateWithAPPID:(NSString *)appId withBundleId:(NSString *)bundelId block:(UpdateBlock)block{
NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
__block NSString *currentVersion=infoDic[@"CFBundleShortVersionString"];
NSURLRequest *request;
if (appId != nil) {
request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",appId]]];
NSLog(@"【1】當前為APPID檢測,您設定的APPID為:%@ 當前版本號為:%@",appId,currentVersion);
}else if (bundelId != nil){
request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?bundleId=%@&country=cn",bundelId]]];
NSLog(@"【1】當前為BundelId檢測,您設定的bundelId為:%@ 當前版本號為:%@",bundelId,currentVersion);
}else{
NSString *currentBundelId=infoDic[@"CFBundleIdentifier"];
request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?bundleId=%@&country=cn",currentBundelId]]];
NSLog(@"【1】當前為自動檢測檢測, 當前版本號為:%@",currentVersion);
}
NSURLSession *session = [NSURLSession sharedSession];
NSLog(@"【2】開始檢測...");
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"【3】檢測失敗,原因:\n%@",error);
dispatch_async(dispatch_get_main_queue(), ^{
block(currentVersion,@"",@"",NO);
});
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
if ([appInfoDic[@"resultCount"] integerValue] == 0) {
NSLog(@"檢測出未上架的APP或者查詢不到");
block(currentVersion,@"",@"",NO);
return;
}
NSLog(@"【3】蘋果伺服器返回的檢測結果:\n appId = %@ \n bundleId = %@ \n 開發賬號名字 = %@ \n 商店版本號 = %@ \n 應用名稱 = %@ \n 開啟連線 = %@",appInfoDic[@"results"][0][@"artistId"],appInfoDic[@"results"][0][@"bundleId"],appInfoDic[@"results"][0][@"artistName"],appInfoDic[@"results"][0][@"version"],appInfoDic[@"results"][0][@"trackName"],appInfoDic[@"results"][0][@"trackViewUrl"]);
NSString *appStoreVersion = appInfoDic[@"results"][0][@"version"];
currentVersion = [currentVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
if (currentVersion.length==2) {
currentVersion = [currentVersion stringByAppendingString:@"0"];
}else if (currentVersion.length==1){
currentVersion = [currentVersion stringByAppendingString:@"00"];
}
appStoreVersion = [appStoreVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
if (appStoreVersion.length==2) {
appStoreVersion = [appStoreVersion stringByAppendingString:@"0"];
}else if (appStoreVersion.length==1){
appStoreVersion = [appStoreVersion stringByAppendingString:@"00"];
}
if([currentVersion floatValue] < [appStoreVersion floatValue])
{
NSLog(@"【4】判斷結果:當前版本號%@ < 商店版本號%@ 需要更新\n=========我是分割線========",[[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"],appInfoDic[@"results"][0][@"version"]);
block(currentVersion,appInfoDic[@"results"][0][@"version"],appInfoDic[@"results"][0][@"trackViewUrl"],YES);
}else{
NSLog(@"【4】判斷結果:當前版本號%@ > 商店版本號%@ 不需要更新\n========我是分割線========",[[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"],appInfoDic[@"results"][0][@"version"]);
block(currentVersion,appInfoDic[@"results"][0][@"version"],appInfoDic[@"results"][0][@"trackViewUrl"],NO);
}
});
}];
[task resume];
}
使用
[[CFPublicTool shareInstance] updateWithAPPID:@"**********" withBundleId:nil block:^(NSString *currentVersion, NSString *storeVersion, NSString *openUrl, BOOL isUpdate) {
if (isUpdate) {
NSString * alertContent = [NSString stringWithFormat:@"檢測到新版本%@,是否更新?",storeVersion];
// 給出提示是否前往 AppStore 更新
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"檢測更新" message:alertContent preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"前往" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
if (openUrl) {
NSURL *appStoreURL = [NSURL URLWithString:openUrl];
if ([[UIApplication sharedApplication] canOpenURL:appStoreURL]) {
[[UIApplication sharedApplication] openURL:appStoreURL];
}
}
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
}else{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"暫無版本更新");
});
}
}];
相關文章
- iOS APP檢查版本更新iOSAPP
- uniapp 打包IOS 更新AppStore版本APPiOS
- iOS版本更新比較及廣告頁展示判斷iOS
- swoole 版本更新
- 版本迭代更新
- openssh版本更新與說明 openssl版本更新與說明
- Mac 更新 PHP 版本MacPHP
- yum更新php版本PHP
- iOS微信更新6.7.4版本:適配iPhone XS MAX大螢幕iOSiPhone
- 判斷 iOS 版本iOS
- iOS版支付寶9.6.6更新 iOS版9.6.6更新日誌iOS
- Mac 系統更新 PHP 版本MacPHP
- CloudQuery v1.3.4 版本更新Cloud
- 自定義版本更新彈窗
- layui更新版本UI
- iOS 的更新提醒教程iOS
- Node 快速切換版本、版本回退(降級)、版本更新(升級)
- ios版Office Lens 迎來1.3版本更新 手機秒變掃描器iOS
- iOS (實現檢查更新功能時)獲取App最新版本號iOSAPP
- ios9.3.3更新了什麼 ios9.3.3更新內容彙總iOS
- SoloPi v0.10.2 版本更新
- collection庫更新1.4.0版本
- SoloPi v0.11.1 版本更新
- Maven更新子模組的版本號Maven
- PHP再次更新Bug修復版本PHP
- Maven依賴版本更新踩坑Maven
- Linux下更新curl版本教程!Linux
- Win10 Mobile/iOS版《OneDrive》迎重大更新版本:新增新功能選項Win10iOS
- iOS熱更新實現方式iOS
- iOS FMDB 查詢 批量更新iOS
- iOS利用iTunesLookup檢查更新iOS
- iOS Cocoapods版本號概念iOS
- iOS 版本號的比較iOS
- iOS9.3.2更新升級有bug 為何iOS更新總是有問題?iOS
- iOS版本相容以及部分iOS 11適配iOS
- Httprunner3.x 版本更新了好多HTTP
- [提問交流]OneThink 版本更新了
- React UI 庫: React Suite 3.8.0 版本更新ReactUI