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

Small fixes #31

Open
wants to merge 5 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
12 changes: 6 additions & 6 deletions Appirater.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,33 +51,33 @@ extern NSString *const kAppiraterDeclinedToRate;
/*
Your app's name.
*/
#define APPIRATER_APP_NAME [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleNameKey]
#define APPIRATER_APP_NAME [[[NSBundle mainBundle] infoDictionary] objectForKey: (NSString *) kCFBundleNameKey]

/*
This is the message your users will see once they've passed the day+launches
threshold.
*/
#define APPIRATER_MESSAGE [NSString stringWithFormat:@"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!", APPIRATER_APP_NAME]
#define APPIRATER_MESSAGE [NSString stringWithFormat: NSLocalizedString(@"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!", nil), APPIRATER_APP_NAME]

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

/*
The text of the button that rejects reviewing the app.
*/
#define APPIRATER_CANCEL_BUTTON @"No, Thanks"
#define APPIRATER_CANCEL_BUTTON NSLocalizedString(@"No Thanks", nil)

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

/*
Text for button to remind the user to review later.
*/
#define APPIRATER_RATE_LATER @"Remind me later"
#define APPIRATER_RATE_LATER NSLocalizedString(@"Remind Me Later", nil)

/*
Users will need to have the same version of your app installed for this many
Expand Down
266 changes: 132 additions & 134 deletions Appirater.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,146 @@
#import <SystemConfiguration/SCNetworkReachability.h>
#include <netinet/in.h>

NSString *const kAppiraterFirstUseDate = @"kAppiraterFirstUseDate";
NSString *const kAppiraterUseCount = @"kAppiraterUseCount";
NSString *const kAppiraterSignificantEventCount = @"kAppiraterSignificantEventCount";
NSString *const kAppiraterCurrentVersion = @"kAppiraterCurrentVersion";
NSString *const kAppiraterRatedCurrentVersion = @"kAppiraterRatedCurrentVersion";
NSString *const kAppiraterDeclinedToRate = @"kAppiraterDeclinedToRate";
NSString *const kAppiraterReminderRequestDate = @"kAppiraterReminderRequestDate";
NSString *const kAppiraterFirstUseDate = @"Appirater.FirstUseDate";
NSString *const kAppiraterUseCount = @"Appirater.UseCount";
NSString *const kAppiraterSignificantEventCount = @"Appirater.SignificantEventCount";
NSString *const kAppiraterCurrentVersion = @"Appirater.CurrentVersion";
NSString *const kAppiraterRatedCurrentVersion = @"Appirater.RatedCurrentVersion";
NSString *const kAppiraterDeclinedToRate = @"Appirater.DeclinedToRate";
NSString *const kAppiraterReminderRequestDate = @"Appirater.ReminderRequestDate";

NSString *templateReviewURL = @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=APP_ID";

#if APPIRATER_APP_ID == 301377083
#error "APPIRATER_APP_ID was not set in \"Appirater.h\""
#endif

@interface Appirater (hidden)
- (BOOL)connectedToNetwork;
@interface Appirater ()
+ (Appirater*)sharedInstance;
- (void)showRatingAlert;
- (BOOL)connectedToNetwork;
- (BOOL)ratingConditionsHaveBeenMet;
+ (void)appWillResignActive;
- (void)hideRatingAlert;
- (void)incrementUseCount;
- (void)showRatingAlert;
@end

@implementation Appirater (hidden)
@implementation Appirater

@synthesize ratingAlert;

- (void)incrementAndRate:(NSNumber*)_canPromptForRating {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

[self incrementUseCount];

if ([_canPromptForRating boolValue] == YES &&
[self ratingConditionsHaveBeenMet] &&
[self connectedToNetwork])
{
[self performSelectorOnMainThread:@selector(showRatingAlert) withObject:nil waitUntilDone:NO];
}

[pool release];
}

- (void)incrementSignificantEventAndRate:(NSNumber*)_canPromptForRating {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

[self incrementSignificantEventCount];

if ([_canPromptForRating boolValue] == YES &&
[self ratingConditionsHaveBeenMet] &&
[self connectedToNetwork])
{
[self performSelectorOnMainThread:@selector(showRatingAlert) withObject:nil waitUntilDone:NO];
}

[pool release];
}

+ (void)appLaunched {
[Appirater appLaunched:YES];
}

+ (void)appLaunched:(BOOL)canPromptForRating {
NSNumber *_canPromptForRating = [[NSNumber alloc] initWithBool:canPromptForRating];
[NSThread detachNewThreadSelector:@selector(incrementAndRate:)
toTarget:[Appirater sharedInstance]
withObject:_canPromptForRating];
[_canPromptForRating release];
}

- (void)hideRatingAlert {
if (self.ratingAlert.visible) {
if (APPIRATER_DEBUG)
NSLog(@"APPIRATER Hiding Alert");
[self.ratingAlert dismissWithClickedButtonIndex:-1 animated:NO];
}
}

+ (void)appWillResignActive {
if (APPIRATER_DEBUG)
NSLog(@"APPIRATER appWillResignActive");
[[Appirater sharedInstance] hideRatingAlert];
}

+ (void)appEnteredForeground:(BOOL)canPromptForRating {
NSNumber *_canPromptForRating = [[NSNumber alloc] initWithBool:canPromptForRating];
[NSThread detachNewThreadSelector:@selector(incrementAndRate:)
toTarget:[Appirater sharedInstance]
withObject:_canPromptForRating];
[_canPromptForRating release];
}

+ (void)userDidSignificantEvent:(BOOL)canPromptForRating {
NSNumber *_canPromptForRating = [[NSNumber alloc] initWithBool:canPromptForRating];
[NSThread detachNewThreadSelector:@selector(incrementSignificantEventAndRate:)
toTarget:[Appirater sharedInstance]
withObject:_canPromptForRating];
[_canPromptForRating release];
}

+ (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];
NSString *reviewURL = [templateReviewURL stringByReplacingOccurrencesOfString:@"APP_ID" withString:[NSString stringWithFormat:@"%d", APPIRATER_APP_ID]];
[userDefaults setBool:YES forKey:kAppiraterRatedCurrentVersion];
[userDefaults synchronize];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:reviewURL]];
#endif
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

switch (buttonIndex) {
case 0:
{
// they don't want to rate it
[userDefaults setBool:YES forKey:kAppiraterDeclinedToRate];
[userDefaults synchronize];
break;
}
case 1:
{
// they want to rate it
[Appirater rateApp];
break;
}
case 2:
// remind them later
[userDefaults setDouble:[[NSDate date] timeIntervalSince1970] forKey:kAppiraterReminderRequestDate];
[userDefaults synchronize];
break;
default:
break;
}
}

#pragma mark - Hidden

- (BOOL)connectedToNetwork {
// Create zero addy
Expand Down Expand Up @@ -85,7 +205,7 @@ - (BOOL)connectedToNetwork {

NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"];
NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0];
NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:self];
NSURLConnection *testConnection = [[[NSURLConnection alloc] initWithRequest:testRequest delegate:self] autorelease];

return ((isReachable && !needsConnection) || nonWiFi) ? (testConnection ? YES : NO) : NO;
}
Expand Down Expand Up @@ -252,125 +372,3 @@ - (void)incrementSignificantEventCount {
}

@end


@interface Appirater ()
- (void)hideRatingAlert;
@end

@implementation Appirater

@synthesize ratingAlert;

- (void)incrementAndRate:(NSNumber*)_canPromptForRating {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

[self incrementUseCount];

if ([_canPromptForRating boolValue] == YES &&
[self ratingConditionsHaveBeenMet] &&
[self connectedToNetwork])
{
[self performSelectorOnMainThread:@selector(showRatingAlert) withObject:nil waitUntilDone:NO];
}

[pool release];
}

- (void)incrementSignificantEventAndRate:(NSNumber*)_canPromptForRating {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

[self incrementSignificantEventCount];

if ([_canPromptForRating boolValue] == YES &&
[self ratingConditionsHaveBeenMet] &&
[self connectedToNetwork])
{
[self performSelectorOnMainThread:@selector(showRatingAlert) withObject:nil waitUntilDone:NO];
}

[pool release];
}

+ (void)appLaunched {
[Appirater appLaunched:YES];
}

+ (void)appLaunched:(BOOL)canPromptForRating {
NSNumber *_canPromptForRating = [[NSNumber alloc] initWithBool:canPromptForRating];
[NSThread detachNewThreadSelector:@selector(incrementAndRate:)
toTarget:[Appirater sharedInstance]
withObject:_canPromptForRating];
[_canPromptForRating release];
}

- (void)hideRatingAlert {
if (self.ratingAlert.visible) {
if (APPIRATER_DEBUG)
NSLog(@"APPIRATER Hiding Alert");
[self.ratingAlert dismissWithClickedButtonIndex:-1 animated:NO];
}
}

+ (void)appWillResignActive {
if (APPIRATER_DEBUG)
NSLog(@"APPIRATER appWillResignActive");
[[Appirater sharedInstance] hideRatingAlert];
}

+ (void)appEnteredForeground:(BOOL)canPromptForRating {
NSNumber *_canPromptForRating = [[NSNumber alloc] initWithBool:canPromptForRating];
[NSThread detachNewThreadSelector:@selector(incrementAndRate:)
toTarget:[Appirater sharedInstance]
withObject:_canPromptForRating];
[_canPromptForRating release];
}

+ (void)userDidSignificantEvent:(BOOL)canPromptForRating {
NSNumber *_canPromptForRating = [[NSNumber alloc] initWithBool:canPromptForRating];
[NSThread detachNewThreadSelector:@selector(incrementSignificantEventAndRate:)
toTarget:[Appirater sharedInstance]
withObject:_canPromptForRating];
[_canPromptForRating release];
}

+ (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];
NSString *reviewURL = [templateReviewURL stringByReplacingOccurrencesOfString:@"APP_ID" withString:[NSString stringWithFormat:@"%d", APPIRATER_APP_ID]];
[userDefaults setBool:YES forKey:kAppiraterRatedCurrentVersion];
[userDefaults synchronize];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:reviewURL]];
#endif
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

switch (buttonIndex) {
case 0:
{
// they don't want to rate it
[userDefaults setBool:YES forKey:kAppiraterDeclinedToRate];
[userDefaults synchronize];
break;
}
case 1:
{
// they want to rate it
[Appirater rateApp];
break;
}
case 2:
// remind them later
[userDefaults setDouble:[[NSDate date] timeIntervalSince1970] forKey:kAppiraterReminderRequestDate];
[userDefaults synchronize];
break;
default:
break;
}
}

@end