-(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];
}
iOS 判断是否是最新版本号
于 2023-05-18 17:36:59 首次发布