Skip to content

Instantly share code, notes, and snippets.

@yuseinishiyama
Last active January 3, 2016 13:09
Show Gist options
  • Save yuseinishiyama/8467768 to your computer and use it in GitHub Desktop.
Save yuseinishiyama/8467768 to your computer and use it in GitHub Desktop.
Fetch latest app version from the app store.
+ (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