iOS 判断是否是最新版本号

这段代码用于检查iOS应用的当前版本,并与AppStore上的最新版本进行对比。如果版本相同,显示已是最新;较旧则提示用户更新;无法连接到iTunesSearchAPI时,显示错误信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

-(void)checkVersion{
    self.view.userInteractionEnabled = NO;
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleLarge];
    activityIndicator.center = self.view.center;
    [self.view addSubview:activityIndicator];
    [activityIndicator startAnimating];
        
    
    NSString *appId = @"YOUR_APP_ID"; // 你的应用在 App Store 的 ID
    NSString *currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];

    NSString *urlString = [NSString stringWithFormat:@"https://itunes.apple.com/lookup?id=%@", appId];
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [activityIndicator stopAnimating];
            [activityIndicator removeFromSuperview];
            self.view.userInteractionEnabled = YES;
        });
        if (data) {
            NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
            NSArray *results = json[@"results"];
            if (results.count > 0) {
                NSDictionary *appInfo = results.firstObject;
                NSString *latestVersion = appInfo[@"version"];
                NSComparisonResult result = [currentVersion compare:latestVersion options:NSNumericSearch];
                CSToastStyle *style = [[CSToastStyle alloc] initWithDefaultStyle];
                style.backgroundColor = [UIColor grayColor];
                style.cornerRadius = 15.0;
                __weak typeof(self) weakSelf = self;
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (result == NSOrderedSame) {
                        // 当前版本与最新版本相同,提示用户是最新版本
                        [weakSelf.view makeToast:@"已是最新版本" duration:1.0 position:CSToastPositionCenter style:style];
                    } else if (result == NSOrderedAscending) {
                        // 当前版本较旧,提示用户前往应用商店更新
                        [weakSelf.view makeToast:@"请前往应用商城更新" duration:1.0 position:CSToastPositionCenter style:style];
                    } else if (result == NSOrderedDescending) {
                        [weakSelf.view makeToast:@"请重试" duration:1.0 position:CSToastPositionCenter style:style];
                    }
                });
            }
        } else {
            NSLog(@"无法连接到 iTunes Search API.");
        }
    }];
    [task resume];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值