iOS 检查App版本

#define DISPATCH_QUEUE_PRIORITY_HIGH 2

NSString *setAppStoreURL = nil;

- (void)checkNewAppVersion:(void(^)(BOOL newVersion, NSString *version))completion {

    NSDictionary *bundleInfo = [[NSBundle mainBundle] infoDictionary];

    NSString *bundleIdentifier = bundleInfo[@"CFBundleIdentifier"];

    NSString *currentVersion = bundleInfo[@"CFBundleShortVersionString"];

    NSURL *lookupURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/lookup?bundleId=%@", bundleIdentifier]];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void) {

        NSData *lookupResults = [NSData dataWithContentsOfURL:lookupURL];

        if (!lookupResults) {

            completion(NO, nil);

            return;

        }

        NSDictionary *jsonResults = [NSJSONSerialization JSONObjectWithData:lookupResults options:0 error:nil];

        dispatch_async(dispatch_get_main_queue(), ^(void) {

            NSUInteger resultCount = [jsonResults[@"resultCount"] integerValue];

            if (resultCount){

                NSDictionary *appDetails = [jsonResults[@"results"] firstObject];

                NSString *appItunesUrl = [appDetails[@"trackViewUrl"] stringByReplacingOccurrencesOfString:@"&uo=4" withString:@""];

                NSString *latestVersion = appDetails[@"version"];

                if ([latestVersion compare:currentVersion options:NSNumericSearch] == NSOrderedDescending) {

                    setAppStoreURL = appItunesUrl;

                    completion(YES, latestVersion);

                } else {

                    completion(NO, nil);

                }

            } else {

                completion(NO, nil);

            }

        });

    });

}

你可能感兴趣的:(object-c,iOS文章)