iOS 獲取App Store 版本號(hào)更新

1、獲取App Store版本信息

//    1、填寫自己App的ID
    NSString * strurl = [[NSString alloc] initWithFormat:@"http://itunes.apple.com/lookup?id=1491881948"];//替換為自己App的ID
    NSURLSession *session=[NSURLSession sharedSession];
    NSURL *url = [NSURL URLWithString:strurl];
    
    //2.創(chuàng)建可變的請(qǐng)求對(duì)象
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error){
        //4.解析數(shù)據(jù)
        NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
        NSArray * results = dict[@"results"];
        NSDictionary * dic = results.firstObject;
        NSString * lineVersion = dic[@"version"];//版本號(hào)
        NSString * releaseNotes = dic[@"releaseNotes"];//更新說明
        NSString * trackViewUrl = dic[@"trackViewUrl"];//鏈接
        DSLog(@"App store版本號(hào):%@",lineVersion);
        DSLog(@"更新說明:%@",releaseNotes);
        DSLog(@"App下載鏈接:%@",trackViewUrl);
        
//        5、獲取本地版本
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
        // 本地app版本
        NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
        
//       6、比較版本信息
        if ([lineVersion floatValue] > [app_Version floatValue]) {
//            7、回到主線程進(jìn)行后續(xù)操作
            dispatch_async(dispatch_get_main_queue(), ^{
                [self setGengxinUI];
            });
            
            DSLog(@"^^%@",app_Version);
        }else{
//            7、回到主線程進(jìn)行后續(xù)操作
            dispatch_async(dispatch_get_main_queue(), ^{
                [SOSVC SOSToolTipShow:@"提示" message:@"已是最新版本" cancelTitle:@"確定" otherTitle:nil];
            });
            
        }
        
    }];
    
    //3.執(zhí)行任務(wù)
    [dataTask resume];

2、跳轉(zhuǎn)App Store和應(yīng)用內(nèi)下載更新app兩種方式

  1. 跳轉(zhuǎn)App Store 下載
  // id99637153*是app的唯一id,對(duì)應(yīng)上文的trackViewUrl
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/%E4%B8%9C%E6%96%B9%E7%BE%8E%E9%A3%9F/id99637153*?mt=8"] options:@{} completionHandler:nil];

2.應(yīng)用內(nèi)更新下載

導(dǎo)入頭文件: #import <StoreKit/StoreKit.h>
準(zhǔn)守協(xié)議:<SKStoreProductViewControllerDelegate>

1.實(shí)現(xiàn)代理SKStoreProductViewControllerDelegate

SKStoreProductViewController *storeProductViewContorller = [[SKStoreProductViewController alloc] init];
storeProductViewContorller.delegate = self;
[storeProductViewContorller loadProductWithParameters: @{SKStoreProductParameterITunesItemIdentifier : @"996371534"} completionBlock:^(BOOL result, NSError *error) {
    if(error){
         NSLog(@"錯(cuò)誤%@",error);
    }else{
        //應(yīng)用界面
        [self.window.rootViewController presentViewController:storeProductViewContorller animated:YES completion:nil];
    }
}];

//實(shí)現(xiàn)取消協(xié)議
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController{
[self.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
[self disMiss];
}

原文鏈接:http://www.itdecent.cn/p/d2dd7d2f37b2
鏈接:http://www.itdecent.cn/p/0190e5234af2

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容