Skip to content

Instantly share code, notes, and snippets.

@jessedc
Created October 19, 2014 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessedc/1e106e52f72f7795c821 to your computer and use it in GitHub Desktop.
Save jessedc/1e106e52f72f7795c821 to your computer and use it in GitHub Desktop.
Apple App Store Viewer (iOS 6+)
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface CMInfoAppStoreViewer : NSObject
- (void)displayAppStoreForApp:(CMAppType)app withNavigationController:(UINavigationController *)navigationController;
- (void)displayAppStoreForAppId:(NSNumber *)appNumber iTunesURL:(NSURL *)itunesURL withNavigationController:(UINavigationController *)navigationController;
@end
#import "CMInfoAppStoreViewer.h"
#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>
#import "WorkingOverlayView.h"
#import "NSURL+AppStoreURL.h"
@interface CMInfoAppStoreViewer() <SKStoreProductViewControllerDelegate, UIAlertViewDelegate>
@property (nonatomic, strong) SKStoreProductViewController *productController;
@property (nonatomic, strong) CMWorkingOverlayViewPresenter *overlayPresenter;
@property (nonatomic, assign, getter=isCancelled) BOOL cancelled;
@end
@implementation CMInfoAppStoreViewer
- (void)displayAppStoreForApp:(CMAppType)app withNavigationController:(UINavigationController *)navigationController
{
//FIXME: JC - although this is in one place, it could be improved.
NSNumber *appNumber = nil;
NSString *appURL = nil;
switch (app)
{
case CMAppTypeSeoul:
{
appNumber = @(343491689);
appURL = @"/us/app/seoul-city-metro-seoul-south/id343491689";
break;
}
case CMAppTypeSeoulLite:
{
appNumber = @(356066122);
appURL = @"/us/app/seoul-city-metro-lite-seoul/id356066122";
break;
}
case CMAppTypeBusan:
{
appNumber = @(356337150);
appURL = @"/us/app/busan-city-metro-south-korean/id356337150";
break;
}
case CMAppTypeDelhi:
{
appNumber = @(377654366);
appURL = @"/us/app/delhi-city-metro-xix-commonwealth/id377654366";
break;
}
case CMAppTypeDubai:
{
appNumber = @(400118556);
appURL = @"/us/app/dubai-city-metro/id400118556";
break;
}
}
[self displayAppStoreForAppId:appNumber
iTunesURL:[NSURL appStoreURLWithPath:appURL]
withNavigationController:navigationController];
}
- (void)displayAppStoreForAppId:(NSNumber *)appNumber iTunesURL:(NSURL *)itunesURL withNavigationController:(UINavigationController *)navigationController
{
NSParameterAssert(appNumber);
NSParameterAssert(itunesURL);
NSAssert([navigationController isKindOfClass:[UINavigationController class]], @"Refernce must be a UINavigationController");
WorkingOverlayView *workingOverlayView = [WorkingOverlayView overlayView];
[workingOverlayView setText:NSLocalizedString(@"RootView.UpgradeCell.LoadingAppStoreMessage", @"Loading App Store Message after tapping the cell")];
self.overlayPresenter = [[CMWorkingOverlayViewPresenter alloc] init];
__weak CMWorkingOverlayViewPresenter *weak_pesenter = self.overlayPresenter;
__weak __typeof(self) weak_self = self;
self.overlayPresenter.overlayTapBlock = ^{
weak_self.cancelled = YES;
[weak_pesenter dismissOverlayViewAnimated:YES];
};
self.productController = [[SKStoreProductViewController alloc] initWithNibName:nil bundle:nil];
self.productController.delegate = self;
__weak __typeof(self.productController) weak_productController = self.productController;
NSDictionary *params = @{ SKStoreProductParameterITunesItemIdentifier: appNumber };
[self.productController loadProductWithParameters:params completionBlock:^(BOOL result, NSError *error) {
[weak_self.overlayPresenter dismissOverlayViewAnimated:NO];
if (result)
{
if (!weak_self.isCancelled)
{
[navigationController presentViewController:weak_productController animated:YES completion:nil];
}
}
else
{
if ([[UIApplication sharedApplication] canOpenURL:itunesURL])
{
[[UIApplication sharedApplication] openURL:itunesURL];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Error",@"Error Message Title")
message:NSLocalizedString(@"no appstore message",@"The App Store is not available from your device.")
delegate:weak_self
cancelButtonTitle:NSLocalizedString(@"Dismiss",@"Dismiss Button Title")
otherButtonTitles:nil];
[alert show];
}
}
}];
[self.overlayPresenter presentOverlayView:workingOverlayView insideView:navigationController.view];
}
#pragma mark - SKStoreProductViewControllerDelegate
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController
{
[viewController dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - UIAlertVieDelegate
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
[self.overlayPresenter dismissOverlayViewAnimated:YES];
}
@end
#import <Foundation/Foundation.h>
@interface NSURL (AppStoreURL)
+ (NSURL *)appStoreURLWithPath:(NSString *)path;
@end
#import "NSURL+AppStoreURL.h"
@implementation NSURL (AppStoreURL)
+ (NSURL *)appStoreURLWithPath:(NSString *)path
{
NSURLComponents *urlComps = [[NSURLComponents alloc] init];
urlComps.host = @"itunes.apple.com";
urlComps.path = path;
if (NSFoundationVersionNumber >NSFoundationVersionNumber_iOS_7_1)
{
urlComps.scheme = @"https";
NSURLQueryItem *mtQUeryItem = [NSURLQueryItem queryItemWithName:@"mt" value:@"8"];
urlComps.queryItems = @[mtQUeryItem];
}
else
{
urlComps.scheme = @"itms";
urlComps.query = @"mt=8";
}
return [urlComps URL];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment