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(@"暫無版本更新");
});
}
}];
相關文章
- uniapp 打包IOS 更新AppStore版本APPiOS
- swoole 版本更新
- openssh版本更新與說明 openssl版本更新與說明
- Mac 更新 PHP 版本MacPHP
- iOS微信更新6.7.4版本:適配iPhone XS MAX大螢幕iOSiPhone
- iOS Cocoapods版本號概念iOS
- iOS (實現檢查更新功能時)獲取App最新版本號iOSAPP
- Linux下更新curl版本教程!Linux
- SoloPi v0.12.0 版本更新
- SoloPi v0.10.2 版本更新
- Mac 系統更新 PHP 版本MacPHP
- SoloPi v0.11.1 版本更新
- CloudQuery v1.3.4 版本更新Cloud
- layui更新版本UI
- 自定義版本更新彈窗
- iOS 版本號的比較iOS
- Node 快速切換版本、版本回退(降級)、版本更新(升級)
- Maven依賴版本更新踩坑Maven
- collection庫更新1.4.0版本
- [提問交流]OneThink 版本更新了
- 如何更新composer包的git版本Git
- iOS 應用版本資訊 BundleVersioniOS
- iOS11.4正式版更新內容大全 iOS11.4正式版更新了什麼?iOS
- iOS12預覽版更新失敗怎麼辦? iOS12無法安裝更新iOS
- 如何在Android studio中更新sdk版本和build-tools版本AndroidUI
- Apache DolphinScheduler 3.3.0 版本重磅更新提前看!Apache
- DolphinScheduler 3.3.0版本更新一覽
- Httprunner3.x 版本更新了好多HTTP
- YC-Framework版本更新:V1.0.5Framework
- YC-Framework版本更新:V1.0.6Framework
- YC-Framework版本更新:V1.0.3Framework
- Bootstrap Blazor 更新版本 5.6.0bootBlazor
- React UI 庫: React Suite 3.8.0 版本更新ReactUI
- 新版本linux更新hostname的方法Linux
- iOS 關閉系統自動更新iOS
- 如何遮蔽 iOS 軟體自動更新,去除更新通知和標記iOS
- 【VMware VCF】更新 VCF 5.1 至 VCF 5.2 版本。
- angular版本更新與配置檔案問題Angular