虽然苹果禁止应用内更新,但是有时候仍需要弹出更新提示。但是iOS审核需要时间,如何实时获取信息判断是否更新呢。我们可以获取App Store中app的信息来判断。代码如下:
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { // 1.直接请求苹果商店信息 [self getAppStoreAppInfoWithAppID:APPID]; } - (void)getAppStoreAppInfoWithAppID:(NSString *)appId { AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; // 可不写 [manager.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil]]; // 必传信息 NSDictionary *dict = @{@"id":appId};//此处的Apple ID [manager POST:@"https://itunes.apple.com/lookup" parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSArray *array = responseObject[@"results"]; if (array.count != 0) {// 先判断返回的数据是否为空 NSDictionary *dict = array[0]; NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary]; NSString *currentAppVersion = infoDic[@"CFBundleShortVersionString"]; NSString *newVersion =[NSString stringWithFormat:@"%@版本更新",dict[@"version"]]; // 也可以判断不一样就行。那样简单 if ([dict[@"version"] compare:currentAppVersion options:NSNumericSearch] == NSOrderedDescending) { NSLog(@"需要更新"); } else { NSLog(@"不需要更新"); } } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }]; }
返回结果图如下: