Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for rating prompt not showing when thresholds are hit #86

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 36 additions & 40 deletions Appirater.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#define SHOW_APPIRATER_DEBUG_ALERTS 0

/*
This file is part of Appirater.

Expand Down Expand Up @@ -46,45 +48,6 @@ extern NSString *const kAppiraterRatedCurrentVersion;
extern NSString *const kAppiraterDeclinedToRate;
extern NSString *const kAppiraterReminderRequestDate;

/*!
Your localized app's name.
*/
#define APPIRATER_LOCALIZED_APP_NAME [[[NSBundle mainBundle] localizedInfoDictionary] objectForKey:@"CFBundleDisplayName"]

/*!
Your app's name.
*/
#define APPIRATER_APP_NAME APPIRATER_LOCALIZED_APP_NAME ? APPIRATER_LOCALIZED_APP_NAME : [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]

/*!
This is the message your users will see once they've passed the day+launches
threshold.
*/
#define APPIRATER_LOCALIZED_MESSAGE NSLocalizedStringFromTableInBundle(@"If you enjoy using %@, would you mind taking a moment to rate it? It won't take more than a minute. Thanks for your support!", @"AppiraterLocalizable", [Appirater bundle], nil)
#define APPIRATER_MESSAGE [NSString stringWithFormat:APPIRATER_LOCALIZED_MESSAGE, APPIRATER_APP_NAME]

/*!
This is the title of the message alert that users will see.
*/
#define APPIRATER_LOCALIZED_MESSAGE_TITLE NSLocalizedStringFromTableInBundle(@"Rate %@", @"AppiraterLocalizable", [Appirater bundle], nil)
#define APPIRATER_MESSAGE_TITLE [NSString stringWithFormat:APPIRATER_LOCALIZED_MESSAGE_TITLE, APPIRATER_APP_NAME]

/*!
The text of the button that rejects reviewing the app.
*/
#define APPIRATER_CANCEL_BUTTON NSLocalizedStringFromTableInBundle(@"No, Thanks", @"AppiraterLocalizable", [Appirater bundle], nil)

/*!
Text of button that will send user to app review page.
*/
#define APPIRATER_LOCALIZED_RATE_BUTTON NSLocalizedStringFromTableInBundle(@"Rate %@", @"AppiraterLocalizable", [Appirater bundle], nil)
#define APPIRATER_RATE_BUTTON [NSString stringWithFormat:APPIRATER_LOCALIZED_RATE_BUTTON, APPIRATER_APP_NAME]

/*!
Text for button to remind the user to review later.
*/
#define APPIRATER_RATE_LATER NSLocalizedStringFromTableInBundle(@"Remind me later", @"AppiraterLocalizable", [Appirater bundle], nil)

@interface Appirater : NSObject <UIAlertViewDelegate, SKStoreProductViewControllerDelegate> {

UIAlertView *ratingAlert;
Expand Down Expand Up @@ -228,7 +191,7 @@ extern NSString *const kAppiraterReminderRequestDate;
/*!
Set the delegate if you want to know when Appirater does something
*/
+ (void)setDelegate:(id<AppiraterDelegate>)delegate;
+ (void) setDelegate:(id<AppiraterDelegate>)delegate;

/*!
Set whether or not Appirater uses animation (currently respected when pushing modal StoreKit rating VCs).
Expand All @@ -247,6 +210,39 @@ extern NSString *const kAppiraterReminderRequestDate;
*/
+ (void)setAlwaysUseMainBundle:(BOOL)useMainBundle;


/*
Your app's name. Defaults to the name set in the application info plist
*/
+ (void) setAppName:(NSString*)appName;

/*
This is the message your users will see once they've passed the day+launches
threshold.
*/
+ (void) setMessage:(NSString*)message;

/*
This is the title of the message alert that users will see.
*/
+ (void) setTitle:(NSString*)title;

/*
Text of button that will send user to app review page.
*/
+ (void) setRateButtonText:(NSString*)rateButtonText;

/*
The text of the button that rejects reviewing the app.
*/
+ (void) setCancelButtonText:(NSString*)cancelButtonText;

/*
Text for button to remind the user to review later.
*/
+ (void) setRateLaterButtonText:(NSString*)rateLaterButtonText;


@end


Expand Down
141 changes: 133 additions & 8 deletions Appirater.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
static NSInteger _significantEventsUntilPrompt = -1;
static double _timeBeforeReminding = 1;
static BOOL _debug = NO;
static NSString *_appName;
static NSString *_message;
static NSString *_title;
static NSString *_rateButtonText;
static NSString *_cancelButtonText;
static NSString *_rateLaterButtonText;
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_5_0
static id<AppiraterDelegate> _delegate;
#else
Expand All @@ -69,6 +75,7 @@
static BOOL _modalOpen = false;
static BOOL _alwaysUseMainBundle = NO;


@interface Appirater ()
- (BOOL)connectedToNetwork;
+ (Appirater*)sharedInstance;
Expand Down Expand Up @@ -157,6 +164,89 @@ - (id)init {
return self;
}

+ (void) setAppName:(NSString*)appName {
_appName = appName;
}
+ (NSString*) appName {
if (_appName) {
return _appName;
}
NSString *kCFBundleDisplayNameKey = @"CFBundleDisplayName";
NSBundle *mainBundle = [NSBundle mainBundle];
NSDictionary *localizedInfo = [mainBundle localizedInfoDictionary];

NSString *localizedName = [localizedInfo objectForKey:kCFBundleDisplayNameKey];
if (localizedName) { // display name is not a required info field
return localizedName;
}
localizedName = [localizedInfo objectForKey:(NSString*)kCFBundleNameKey];
if (localizedName) {
return localizedName;
}

// fall back on non-localized name
NSDictionary *info = [mainBundle infoDictionary];
NSString *name = [info objectForKey:kCFBundleDisplayNameKey];
if (name) {
return name;
}
return [info objectForKey:(NSString*)kCFBundleNameKey];
}

+ (void) setMessage:(NSString*)message {
_message = message;
}
+ (NSString*) message {
if (_message) {
return _message;
}
NSString *messageFormat = NSLocalizedStringFromTable(@"If you enjoy using %@, would you mind taking a moment to rate it? It won't take more than a minute. Thanks for your support!",
@"AppiraterLocalizable", nil);
return [NSString stringWithFormat:messageFormat, [self appName]];
}

+ (void) setTitle:(NSString*)title {
_title = title;
}
+ (NSString*) title {
if (_title) {
return _title;
}
NSString *titleFormat = NSLocalizedStringFromTable(@"Rate %@", @"AppiraterLocalizable", nil);
return [NSString stringWithFormat:titleFormat, [self appName]];
}

+ (void) setRateButtonText:(NSString*)rateButtonText {
_rateButtonText = rateButtonText;
}
+ (NSString*) rateButtonText {
if (_rateButtonText) {
return _rateButtonText;
}
NSString *rateButtonTextFormat = NSLocalizedStringFromTable(@"Rate %@", @"AppiraterLocalizable", nil);
return [NSString stringWithFormat:rateButtonTextFormat, [self appName]];
}

+ (void) setCancelButtonText:(NSString*)cancelButtonText {
_cancelButtonText = cancelButtonText;
}
+ (NSString*) cancelButtonText {
if (_cancelButtonText) {
return _cancelButtonText;
}
return NSLocalizedStringFromTable(@"No, Thanks", @"AppiraterLocalizable", nil);
}

+ (void) setRateLaterButtonText:(NSString*)rateLaterButtonText {
_rateLaterButtonText = rateLaterButtonText;
}
+ (NSString*) rateLaterButtonText {
if (_rateLaterButtonText) {
return _rateLaterButtonText;
}
return NSLocalizedStringFromTable(@"Remind me later", @"AppiraterLocalizable", nil);
}

- (BOOL)connectedToNetwork {
// Create zero addy
struct sockaddr_in zeroAddress;
Expand Down Expand Up @@ -205,11 +295,11 @@ + (Appirater*)sharedInstance {
}

- (void)showRatingAlert {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:APPIRATER_MESSAGE_TITLE
message:APPIRATER_MESSAGE
delegate:self
cancelButtonTitle:APPIRATER_CANCEL_BUTTON
otherButtonTitles:APPIRATER_RATE_BUTTON, APPIRATER_RATE_LATER, nil];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[Appirater title]
message:[Appirater message]
delegate:self
cancelButtonTitle:[Appirater cancelButtonText]
otherButtonTitles:[Appirater rateButtonText], [Appirater rateLaterButtonText], nil];
self.ratingAlert = alertView;
[alertView show];

Expand Down Expand Up @@ -366,7 +456,34 @@ - (void)incrementAndRate:(BOOL)canPromptForRating {
^{
[self showRatingAlert];
});
}
} else {
#if SHOW_APPIRATER_DEBUG_ALERTS
[self showProgressTowardsPrompt];
#endif
}
}

- (void) showProgressTowardsPrompt {
dispatch_async(dispatch_get_main_queue(), ^{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSInteger numberOfEvents = [userDefaults integerForKey:kAppiraterSignificantEventCount];
NSInteger numberOfLaunches = [userDefaults integerForKey:kAppiraterUseCount];
NSDate *dateOfFirstLaunch = [NSDate dateWithTimeIntervalSince1970:[userDefaults doubleForKey:kAppiraterFirstUseDate]];
const double daysSinceFirstLaunch = [[NSDate date] timeIntervalSinceDate:dateOfFirstLaunch] / 60 / 60;

NSString *message = [NSString stringWithFormat:@"%ld significant events so far (%ld to trigger rating prompt)\n\n"
@"%ld launches so far (%ld to trigger)\n\n"
@"%0.2f days since app install (%0.2f to trigger)",
(long)numberOfEvents, (long)_significantEventsUntilPrompt,
(long)numberOfLaunches, (long)_usesUntilPrompt,
daysSinceFirstLaunch, _daysUntilPrompt];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Appirater debug message"
message:message
delegate:nil
cancelButtonTitle:@"Ok!"
otherButtonTitles:nil];
[alertView show];
});
}

- (void)incrementSignificantEventAndRate:(BOOL)canPromptForRating {
Expand All @@ -380,7 +497,11 @@ - (void)incrementSignificantEventAndRate:(BOOL)canPromptForRating {
^{
[self showRatingAlert];
});
}
} else {
#if SHOW_APPIRATER_DEBUG_ALERTS
[self showProgressTowardsPrompt];
#endif
}
}

- (BOOL)userHasDeclinedToRate {
Expand Down Expand Up @@ -506,7 +627,11 @@ + (void)rateApp {
} else {

#if TARGET_IPHONE_SIMULATOR
NSLog(@"APPIRATER NOTE: iTunes App Store is not supported on the iOS simulator. Unable to open App Store page.");
[[[UIAlertView alloc] initWithTitle:nil
message:@"APPIRATER NOTE: iTunes App Store is not supported on the iOS simulator."
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil] show];
#else
NSString *reviewURL = [templateReviewURL stringByReplacingOccurrencesOfString:@"APP_ID" withString:[NSString stringWithFormat:@"%@", _appId]];

Expand Down