Last active
April 12, 2018 08:47
-
-
Save enigmatic7earth/9a88c6d672f4f9ace4602810b20293db to your computer and use it in GitHub Desktop.
Check AppStore if an update for app is available.
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
-(void) checkForAppUpdate{ | |
BOOL updateAvailable = NO; | |
NSDictionary* infoDictionary = [[NSBundle mainBundle] infoDictionary]; | |
NSString* appID = infoDictionary[@"CFBundleIdentifier"]; | |
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?bundleId=%@", appID]]; | |
NSData* data = [NSData dataWithContentsOfURL:url]; | |
NSDictionary* lookup = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; | |
if ([lookup[@"resultCount"] integerValue] == 1){ | |
NSString* appStoreVersion = lookup[@"results"][0][@"version"]; | |
NSString* currentVersion = infoDictionary[@"CFBundleShortVersionString"]; | |
if (![appStoreVersion isEqualToString:currentVersion]){ | |
//NSLog(@"Need to update [%@ != %@]", appStoreVersion, currentVersion); | |
updateAvailable = YES; | |
NSString * appITunesItemIdentifier = lookup[@"results"][0][@"trackId"]; | |
[self openStoreProductViewControllerWithITunesItemIdentifier:[appITunesItemIdentifier intValue]]; | |
} | |
} | |
updateAvailable = NO; | |
} | |
- (void)openStoreProductViewControllerWithITunesItemIdentifier:(NSInteger)iTunesItemIdentifier { | |
SKStoreProductViewController *storeViewController = [[SKStoreProductViewController alloc] init]; | |
storeViewController.delegate = self; | |
NSNumber *identifier = [NSNumber numberWithInteger:iTunesItemIdentifier]; | |
NSDictionary *parameters = @{ SKStoreProductParameterITunesItemIdentifier:identifier }; | |
UIViewController *viewController = self.window.rootViewController; | |
[storeViewController loadProductWithParameters:parameters | |
completionBlock:^(BOOL result, NSError *error) { | |
if (result) | |
[viewController presentViewController:storeViewController | |
animated:YES | |
completion:nil]; | |
else NSLog(@"SKStoreProductViewController: %@", error); | |
}]; | |
} | |
#pragma mark - SKStoreProductViewControllerDelegate | |
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController { | |
[viewController dismissViewControllerAnimated:YES completion:nil]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment