Last active
January 3, 2016 13:09
-
-
Save yuseinishiyama/8467768 to your computer and use it in GitHub Desktop.
Fetch latest app version from the app store.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (NSString *)applicationVersion { | |
return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; | |
} | |
+ (void)showNeedUpdateAlertIfNeeded | |
{ | |
[self getLatestAppVersionAsynchronousWithCompletionBlock:^(NSString *latestAppVersion) { | |
if ([latestAppVersion compare:[ApplicationInformation applicationVersion] options:NSNumericSearch] == NSOrderedDescending) { | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
// Show alert etc... | |
}); | |
} | |
}]; | |
} | |
+ (void)getLatestAppVersionAsynchronousWithCompletionBlock:(void(^)(NSString *))completionBlock | |
{ | |
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@", kAppStoreID]] | |
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData | |
timeoutInterval:20.0]; | |
[NSURLConnection sendAsynchronousRequest:request | |
queue:[[NSOperationQueue alloc] init] | |
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { | |
if (data) { | |
NSDictionary *versionSummary = [NSJSONSerialization JSONObjectWithData:data | |
options:NSJSONReadingAllowFragments | |
error:&error]; | |
NSDictionary *results = [[versionSummary objectForKey:@"results"] objectAtIndex:0]; | |
NSString *latestVersion = [results objectForKey:@"version"]; | |
NSLog(@">>>>> Latest App Version is %@.", latestVersion); | |
completionBlock(latestVersion); | |
} else { | |
NSLog(@">>>>> Fail to Get Latest App Version."); | |
} | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment