Skip to content

Commit

Permalink
Added optional support for iOS 6's StoreKit to allow rating without l…
Browse files Browse the repository at this point in the history
…eaving the app.
  • Loading branch information
ngreenstein committed Jan 12, 2013
1 parent 1776888 commit 5f82bec
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Appirater.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#import <Foundation/Foundation.h>
#import "AppiraterDelegate.h"
#import <StoreKit/StoreKit.h>

extern NSString *const kAppiraterFirstUseDate;
extern NSString *const kAppiraterUseCount;
Expand Down Expand Up @@ -84,7 +85,7 @@ extern NSString *const kAppiraterReminderRequestDate;
*/
#define APPIRATER_RATE_LATER NSLocalizedStringFromTable(@"Remind me later", @"AppiraterLocalizable", nil)

@interface Appirater : NSObject <UIAlertViewDelegate> {
@interface Appirater : NSObject <UIAlertViewDelegate, SKStoreProductViewControllerDelegate> {

UIAlertView *ratingAlert;
}
Expand Down
45 changes: 36 additions & 9 deletions Appirater.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
static double _timeBeforeReminding = 1;
static BOOL _debug = NO;
static id<AppiraterDelegate> _delegate;
static UIStatusBarStyle _statusBarStyle;

@interface Appirater ()
- (BOOL)connectedToNetwork;
Expand Down Expand Up @@ -95,6 +96,9 @@ + (void) setDebug:(BOOL)debug {
+ (void)setDelegate:(id<AppiraterDelegate>)delegate{
_delegate = delegate;
}
+ (void)setStatusBarStyle:(UIStatusBarStyle)style {
_statusBarStyle = style;
}

- (BOOL)connectedToNetwork {
// Create zero addy
Expand Down Expand Up @@ -361,18 +365,34 @@ + (void)userDidSignificantEvent:(BOOL)canPromptForRating {
}

+ (void)rateApp {
#if TARGET_IPHONE_SIMULATOR
NSLog(@"APPIRATER NOTE: iTunes App Store is not supported on the iOS simulator. Unable to open App Store page.");
#else
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

// this URL Scheme should work in the iOS 6 App Store in addition to older stores
NSString *reviewURL = [templateReviewURL stringByReplacingOccurrencesOfString:@"APP_ID" withString:[NSString stringWithFormat:@"%@", _appId]];

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:YES forKey:kAppiraterRatedCurrentVersion];
[userDefaults synchronize];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:reviewURL]];
#endif

//Use the in-app StoreKit view if available (iOS 6) and imported. This works in the simulator.
if (NSStringFromClass([SKStoreProductViewController class]) != nil) {

SKStoreProductViewController *storeViewController = [[SKStoreProductViewController alloc] init];
NSNumber *appId = [NSNumber numberWithInteger:_appId.integerValue];
[storeViewController loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier:appId} completionBlock:nil];
storeViewController.delegate = self.sharedInstance;
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:storeViewController animated:YES completion:^{
//Temporarily use a black status bar to match the StoreKit view.
[self setStatusBarStyle:[UIApplication sharedApplication].statusBarStyle];
[[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:YES];
}];

//Use the standard openUrl method if StoreKit is unavailable.
} else {

#if TARGET_IPHONE_SIMULATOR
NSLog(@"APPIRATER NOTE: iTunes App Store is not supported on the iOS simulator. Unable to open App Store page.");
#else
NSString *reviewURL = [templateReviewURL stringByReplacingOccurrencesOfString:@"APP_ID" withString:[NSString stringWithFormat:@"%@", _appId]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:reviewURL]];
#endif
}
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
Expand Down Expand Up @@ -411,4 +431,11 @@ - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)butto
}
}

//Close the in-app rating (StoreKit) view and restore the previous status bar style.
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
[[UIApplication sharedApplication]setStatusBarStyle:_statusBarStyle animated:YES];
[[UIApplication sharedApplication].keyWindow.rootViewController dismissViewControllerAnimated:YES completion:nil];
[self.class setStatusBarStyle:(UIStatusBarStyle)nil];
}

@end
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ Getting Started
---------------
1. Add the Appirater code into your project.
2. Add the `CFNetwork` and `SystemConfiguration` frameworks to your project.
3. Call `[Appirater setAppId:@"yourAppId"]` with the app id provided by Apple. A good place to do this is at the beginning of your app delegate's `application:didFinishLaunchingWithOptions:` method.
4. Call `[Appirater appLaunched:YES]` at the end of your app delegate's `application:didFinishLaunchingWithOptions:` method.
5. Call `[Appirater appEnteredForeground:YES]` in your app delegate's `applicationWillEnterForeground:` method.
6. (OPTIONAL) Call `[Appirater userDidSignificantEvent:YES]` when the user does something 'significant' in the app.
3. If you want to take advantage of [StoreKit's in-app rating view](http://developer.apple.com/library/ios/#documentation/StoreKit/Reference/SKITunesProductViewController_Ref/Introduction/Introduction.html) (iOS 6), add the `StoreKit` framework. Be sure to **change Required to Optional** for StoreKit in your target's Build Phases » Link Binary with Libraries section.
4. Call `[Appirater setAppId:@"yourAppId"]` with the app id provided by Apple. A good place to do this is at the beginning of your app delegate's `application:didFinishLaunchingWithOptions:` method.
5. Call `[Appirater appLaunched:YES]` at the end of your app delegate's `application:didFinishLaunchingWithOptions:` method.
6. Call `[Appirater appEnteredForeground:YES]` in your app delegate's `applicationWillEnterForeground:` method.
7. (OPTIONAL) Call `[Appirater userDidSignificantEvent:YES]` when the user does something 'significant' in the app.

Configuration
-------------
Expand Down

0 comments on commit 5f82bec

Please sign in to comment.