iOS开发点滴

iOS 获取App Store 版本号更新

2022-09-16  本文已影响0人  小和大大

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.创建可变的请求对象
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error){
        //4.解析数据
        NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
        NSArray * results = dict[@"results"];
        NSDictionary * dic = results.firstObject;
        NSString * lineVersion = dic[@"version"];//版本号
        NSString * releaseNotes = dic[@"releaseNotes"];//更新说明
        NSString * trackViewUrl = dic[@"trackViewUrl"];//链接
        DSLog(@"App store版本号:%@",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、回到主线程进行后续操作
            dispatch_async(dispatch_get_main_queue(), ^{
                [self setGengxinUI];
            });
            
            DSLog(@"^^%@",app_Version);
        }else{
//            7、回到主线程进行后续操作
            dispatch_async(dispatch_get_main_queue(), ^{
                [SOSVC SOSToolTipShow:@"提示" message:@"已是最新版本" cancelTitle:@"确定" otherTitle:nil];
            });
            
        }
        
    }];
    
    //3.执行任务
    [dataTask resume];

2、跳转App Store和应用内下载更新app两种方式

  1. 跳转App Store 下载
  // id99637153*是app的唯一id,对应上文的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.应用内更新下载

导入头文件: #import <StoreKit/StoreKit.h>
准守协议:<SKStoreProductViewControllerDelegate>

1.实现代理SKStoreProductViewControllerDelegate

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

//实现取消协议
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController{
[self.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
[self disMiss];
}

原文链接:https://www.jianshu.com/p/d2dd7d2f37b2
链接:https://www.jianshu.com/p/0190e5234af2

上一篇下一篇

猜你喜欢

热点阅读