### iOS判断App是否需要更新 无需后台支持

新上传的App可能没法通过http://itunes.apple.com/lookup?id来获取到信息,这样的话就需要后台的接口来支持了

直接获取AppStore上面的App信息,省去了后台单独写接口支持
话不多说,直接上代码

/**
 判断AppStore是否有新版本
 
 @param appId      appId
 @param completion block回调
 */
+ (void)isUpdatedInAppStoreWithAppId:(NSString *)appId Completion:(void (^)(BOOL isUpdate, NSString *newVersion, NSString *updateLog))completion;
+ (void)isUpdatedInAppStoreWithAppId:(NSString *)appId Completion:(void (^)(BOOL, NSString *, NSString *))completion{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",appId]];
    NSString *appInfo = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    NSData *data = [appInfo dataUsingEncoding:NSUTF8StringEncoding];

    NSError *error;
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    if (error) {
        NSLog(@"解析失败:%@",error);
    }
    NSDictionary *infoDict = dict[@"results"][0];
    NSString *updateLog = infoDict[@"releaseNotes"];
    NSString *newVersion = infoDict[@"version"];
    NSString *oldVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    BOOL isUpdate = NO;
    if ([oldVersion compare:newVersion options:NSNumericSearch] == NSOrderedAscending) {
        isUpdate = YES;
    }
    completion(isUpdate,newVersion,updateLog);
}

你可能感兴趣的:(### iOS判断App是否需要更新 无需后台支持)