-
Notifications
You must be signed in to change notification settings - Fork 103
/
ATAppRatingFlow.m
740 lines (649 loc) · 27.4 KB
/
ATAppRatingFlow.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
//
// ATAppRatingFlow.m
// ApptentiveConnect
//
// Created by Andrew Wooster on 7/8/11.
// Copyright 2011 Apptentive, Inc. All rights reserved.
//
#import "ATAppRatingFlow.h"
#import "ATAPIRequest.h"
#import "ATBackend.h"
#import "ATConnect.h"
#import "ATAppConfigurationUpdater.h"
#import "ATFeedback.h"
#import "ATReachability.h"
#import "ATAppRatingMetrics.h"
#import "ATAppRatingFlow_Private.h"
#import "ATUtilities.h"
#import "ATWebClient.h"
static ATAppRatingFlow *sharedRatingFlow = nil;
//TODO: This should be changed for iOS 4+
#define kATAppAppUsageMinimumInterval (20)
#if TARGET_OS_IPHONE
@interface ATAppRatingFlow ()
@property (nonatomic, retain) UIViewController *viewController;
@end
#endif
@interface ATAppRatingFlow ()
/* Days since first app use when the user will first be prompted. */
@property (nonatomic, readonly) NSUInteger daysBeforePrompt;
/* Number of app uses before which the user will first be prompted. */
@property (nonatomic, readonly) NSUInteger usesBeforePrompt;
/* Significant events before the user will be prompted. */
@property (nonatomic, readonly) NSUInteger significantEventsBeforePrompt;
/* Days before the user will be re-prompted after having pressed the "Remind Me Later" button. */
@property (nonatomic, readonly) NSUInteger daysBeforeRePrompting;
@end
@interface ATAppRatingFlow (Private)
- (void)updateLastUseOfApp;
- (void)postNotification:(NSString *)name;
- (void)postNotification:(NSString *)name forButton:(int)button;
- (NSString *)appName;
- (NSURL *)URLForRatingApp;
- (BOOL)requirementsToShowDialogMet;
- (BOOL)shouldShowDialog;
/*! Returns YES if a dialog was shown. */
- (BOOL)showDialogIfNecessary;
- (void)updateVersionInfo;
- (void)userDidUseApp;
- (void)userDidSignificantEvent;
- (void)setRatingDialogWasShown;
- (void)setUserDislikesThisVersion;
- (void)setDeclinedToRateThisVersion;
- (void)setRatedApp;
- (void)logDefaults;
#if TARGET_OS_IPHONE
- (void)appDidFinishLaunching:(NSNotification *)notification;
- (void)appDidEnterBackground:(NSNotification *)notification;
- (void)appWillEnterForeground:(NSNotification *)notification;
- (void)appWillResignActive:(NSNotification *)notification;
- (UIViewController *)rootViewControllerForCurrentWindow;
#endif
- (void)tryToShowDialogWaitingForReachability;
- (void)reachabilityChangedAndPendingDialog:(NSNotification *)notification;
- (void)preferencesChanged:(NSNotification *)notification;
- (void)loadPreferences;
@end
@implementation ATAppRatingFlow
@synthesize daysBeforePrompt, usesBeforePrompt, significantEventsBeforePrompt, daysBeforeRePrompting;
#if TARGET_OS_IPHONE
@synthesize viewController;
#endif
- (id)initWithAppID:(NSString *)anITunesAppID {
if ((self = [super init])) {
[ATAppRatingFlow_Private registerDefaults];
[self loadPreferences];
iTunesAppID = [anITunesAppID retain];
#if TARGET_OS_IPHONE
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidFinishLaunching:) name:UIApplicationDidFinishLaunchingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
#endif
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(preferencesChanged:) name:ATConfigurationPreferencesChangedNotification object:nil];
}
return self;
}
+ (ATAppRatingFlow *)sharedRatingFlowWithAppID:(NSString *)iTunesAppID {
@synchronized(self) {
if (sharedRatingFlow == nil) {
sharedRatingFlow = [[ATAppRatingFlow alloc] initWithAppID:iTunesAppID];
}
return sharedRatingFlow;
}
}
#if TARGET_OS_IPHONE
- (void)appDidLaunch:(BOOL)canPromptForRating viewController:(UIViewController *)vc
#elif TARGET_OS_MAC
- (void)appDidLaunch:(BOOL)canPromptForRating
#endif
{
#if TARGET_OS_IPHONE
self.viewController = vc;
#elif TARGET_OS_MAC
[self userDidUseApp];
#endif
#if TARGET_IPHONE_SIMULATOR
[self logDefaults];
#endif
BOOL showedDialog = NO;
if (canPromptForRating) {
showedDialog = [self showDialogIfNecessary];
}
#if TARGET_OS_IPHONE
if (!showedDialog) {
self.viewController = nil;
}
#endif
}
#if TARGET_OS_IPHONE
- (void)appDidEnterForeground:(BOOL)canPromptForRating viewController:(UIViewController *)vc {
self.viewController = vc;
BOOL showedDialog = NO;
if (canPromptForRating) {
showedDialog = [self showDialogIfNecessary];
}
if (!showedDialog) {
self.viewController = nil;
}
}
#endif
#if TARGET_OS_IPHONE
- (void)userDidPerformSignificantEvent:(BOOL)canPromptForRating viewController:(UIViewController *)vc
#elif TARGET_OS_MAC
- (void)userDidPerformSignificantEvent:(BOOL)canPromptForRating
#endif
{
#if TARGET_OS_IPHONE
self.viewController = vc;
#endif
[self userDidSignificantEvent];
BOOL showedDialog = NO;
if (canPromptForRating) {
showedDialog = [self showDialogIfNecessary];
}
#if TARGET_OS_IPHONE
if (!showedDialog) {
self.viewController = nil;
}
#endif
}
#if TARGET_OS_IPHONE
- (void)showEnjoymentDialog:(UIViewController *)vc
#elif TARGET_OS_MAC
- (IBAction)showEnjoymentDialog:(id)sender
#endif
{
NSString *title = [NSString stringWithFormat:ATLocalizedString(@"Do you love %@?", @"Title for enjoyment alert view. Parameter is app name."), [self appName]];
#if TARGET_OS_IPHONE
self.viewController = vc;
if (!enjoymentDialog) {
enjoymentDialog = [[UIAlertView alloc] initWithTitle:title message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:ATLocalizedString(@"No", @"no"), ATLocalizedString(@"Yes", @"yes"), nil];
[enjoymentDialog show];
}
[self postNotification:ATAppRatingDidPromptForEnjoymentNotification];
[self setRatingDialogWasShown];
#elif TARGET_OS_MAC
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert addButtonWithTitle:ATLocalizedString(@"Yes", @"yes")];
[alert addButtonWithTitle:ATLocalizedString(@"No", @"no")];
[alert setMessageText:title];
[alert setInformativeText:ATLocalizedString(@"You've been using this app for a while. Do you love it?", @"Enjoyment dialog text")];
[alert setAlertStyle:NSInformationalAlertStyle];
[alert setIcon:[NSImage imageNamed:NSImageNameApplicationIcon]];
[self postNotification:ATAppRatingDidPromptForEnjoymentNotification];
[self setRatingDialogWasShown];
NSUInteger result = [alert runModal];
if (result == NSAlertFirstButtonReturn) { // yes
[self showRatingDialog:self];
} else if (result == NSAlertSecondButtonReturn) { // no
[self setUserDislikesThisVersion];
[[ATBackend sharedBackend] setCurrentFeedback:nil];
ATConnect *connection = [ATConnect sharedConnection];
connection.customPlaceholderText = ATLocalizedString(@"What can we do to ensure that you love our app? We appreciate your constructive feedback.", @"Custom placeholder feedback text when user is unhappy with the application.");
ATFeedbackControllerType oldType = connection.feedbackControllerType;
connection.feedbackControllerType = ATFeedbackControllerSimple;
[connection showFeedbackWindow:self];
ATFeedback *inProgressFeedback = [[ATBackend sharedBackend] currentFeedback];
inProgressFeedback.source = ATFeedbackSourceEnjoymentDialog;
connection.customPlaceholderText = nil;
connection.feedbackControllerType = oldType;
}
#endif
}
#if TARGET_OS_IPHONE
- (void)showEnjoymentDialogIfRequirementsMet:(UIViewController *)vc{
self.viewController = vc;
BOOL showedDialog = [self showDialogIfNecessary];
if (!showedDialog) {
self.viewController = nil;
}
}
#endif
#if TARGET_OS_IPHONE
- (void)showRatingDialog:(UIViewController *)vc
#elif TARGET_OS_MAC
- (IBAction)showRatingDialog:(id)sender
#endif
{
NSString *title = ATLocalizedString(@"Thank You", @"Rate app title.");
NSString *message = [NSString stringWithFormat:ATLocalizedString(@"We're so happy to hear that you love %@! It'd be really helpful if you rated us in the App Store. Thanks so much for spending some time with us.", @"Rate app message. Parameter is app name."), [self appName]];
NSString *rateAppTitle = [NSString stringWithFormat:ATLocalizedString(@"Rate %@", @"Rate app button title"), [self appName]];
NSString *noThanksTitle = ATLocalizedString(@"No Thanks", @"cancel title for app rating dialog");
NSString *remindMeTitle = ATLocalizedString(@"Remind Me Later", @"Remind me later button title");
#if TARGET_OS_IPHONE
self.viewController = vc;
if (!ratingDialog) {
ratingDialog = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:noThanksTitle otherButtonTitles:rateAppTitle, remindMeTitle, nil];
[ratingDialog show];
}
[self postNotification:ATAppRatingDidPromptForRatingNotification];
[self setRatingDialogWasShown];
#elif TARGET_OS_MAC
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert addButtonWithTitle:noThanksTitle];
[alert addButtonWithTitle:remindMeTitle];
[alert addButtonWithTitle:rateAppTitle];
[alert setMessageText:title];
[alert setInformativeText:message];
[alert setAlertStyle:NSInformationalAlertStyle];
[alert setIcon:[NSImage imageNamed:NSImageNameApplicationIcon]];
[self postNotification:ATAppRatingDidPromptForRatingNotification];
[self setRatingDialogWasShown];
NSUInteger result = [alert runModal];
if (result == NSAlertFirstButtonReturn) { // cancel
[self setDeclinedToRateThisVersion];
} else if (result == NSAlertSecondButtonReturn) { // remind me
[self setRatingDialogWasShown];
} else if (result == NSAlertThirdButtonReturn) { // rate app
[self openURLForRatingApp];
}
#endif
}
#if TARGET_OS_IPHONE
#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView == enjoymentDialog) {
[enjoymentDialog release], enjoymentDialog = nil;
if (buttonIndex == 0) { // no
[self postNotification:ATAppRatingDidClickEnjoymentButtonNotification forButton:ATAppRatingEnjoymentButtonTypeNo];
[self setUserDislikesThisVersion];
if (!self.viewController) {
NSLog(@"No view controller to present feedback interface!!");
} else {
[[ATBackend sharedBackend] setCurrentFeedback:nil];
ATConnect *connection = [ATConnect sharedConnection];
connection.customPlaceholderText = ATLocalizedString(@"What can we do to ensure that you love our app? We appreciate your constructive feedback.", @"Custom placeholder feedback text when user is unhappy with the application.");
ATFeedbackControllerType oldType = connection.feedbackControllerType;
connection.feedbackControllerType = ATFeedbackControllerSimple;
[connection presentFeedbackControllerFromViewController:self.viewController];
ATFeedback *inProgressFeedback = [[ATBackend sharedBackend] currentFeedback];
inProgressFeedback.source = ATFeedbackSourceEnjoymentDialog;
connection.customPlaceholderText = nil;
self.viewController = nil;
connection.feedbackControllerType = oldType;
}
} else if (buttonIndex == 1) { // yes
[self postNotification:ATAppRatingDidClickEnjoymentButtonNotification forButton:ATAppRatingEnjoymentButtonTypeYes];
[self showRatingDialog:self.viewController];
}
} else if (alertView == ratingDialog) {
[ratingDialog release], ratingDialog = nil;
if (buttonIndex == 1) { // rate
[self postNotification:ATAppRatingDidClickRatingButtonNotification forButton:ATAppRatingButtonTypeRateApp];
[self openURLForRatingApp];
} else if (buttonIndex == 2) { // remind later
[self postNotification:ATAppRatingDidClickRatingButtonNotification forButton:ATAppRatingButtonTypeRemind];
[self setRatingDialogWasShown];
} else if (buttonIndex == 0) { // no thanks
[self postNotification:ATAppRatingDidClickRatingButtonNotification forButton:ATAppRatingButtonTypeNo];
[self setDeclinedToRateThisVersion];
}
self.viewController = nil;
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
NSLog(@"ATAppRatingFlow dismissing alert view %@, %d", alertView, buttonIndex);
if (alertView == enjoymentDialog) {
[enjoymentDialog release], enjoymentDialog = nil;
self.viewController = nil;
} else if (alertView == ratingDialog) {
[ratingDialog release], ratingDialog = nil;
self.viewController = nil;
}
}
#pragma mark SKStoreProductViewControllerDelegate
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)productViewController {
[productViewController dismissModalViewControllerAnimated:YES];
}
#endif
#if TARGET_OS_IPHONE
- (void)showUnableToOpenAppStoreDialog {
UIAlertView *errorAlert = [[[UIAlertView alloc] initWithTitle:ATLocalizedString(@"Oops!", @"Unable to load the App Store title") message:ATLocalizedString(@"Unable to load the App Store", @"Unable to load the App Store message") delegate:nil cancelButtonTitle:ATLocalizedString(@"Okay", @"Okay button title") otherButtonTitles:nil] autorelease];
[errorAlert show];
}
#endif
- (void)openURLForRatingApp {
NSURL *url = [self URLForRatingApp];
[self setRatedApp];
#if TARGET_OS_IPHONE
if ([SKStoreProductViewController class] != NULL && iTunesAppID) {
#if TARGET_IPHONE_SIMULATOR
[self showUnableToOpenAppStoreDialog];
#else
SKStoreProductViewController *vc = [[[SKStoreProductViewController alloc] init] autorelease];
vc.delegate = self;
[vc loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier:iTunesAppID} completionBlock:^(BOOL result, NSError *error) {
if (error) {
[self showUnableToOpenAppStoreDialog];
NSLog(@"Error loading product view: %@", error);
} else {
UIViewController *presentingVC = [self rootViewControllerForCurrentWindow];
[presentingVC presentModalViewController:vc animated:YES];
}
}];
#endif
} else {
if (![[UIApplication sharedApplication] canOpenURL:url]) {
NSLog(@"No application can open the URL: %@", url);
[self showUnableToOpenAppStoreDialog];
}
[[UIApplication sharedApplication] openURL:url];
}
#elif TARGET_OS_MAC
[[NSWorkspace sharedWorkspace] openURL:url];
#endif
}
@end
@implementation ATAppRatingFlow (Private)
- (void)updateLastUseOfApp {
if (lastUseOfApp) {
[lastUseOfApp release], lastUseOfApp = nil;
}
lastUseOfApp = [[NSDate alloc] init];
}
- (void)postNotification:(NSString *)name {
[[NSNotificationCenter defaultCenter] postNotificationName:name object:self];
}
- (void)postNotification:(NSString *)name forButton:(int)button {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:button] forKey:ATAppRatingButtonTypeKey];
[[NSNotificationCenter defaultCenter] postNotificationName:name object:self userInfo:userInfo];
}
- (NSString *)appName {
NSString *displayName = nil;
NSArray *appNameKeys = [NSArray arrayWithObjects:@"CFBundleDisplayName", (NSString *)kCFBundleNameKey, nil];
NSMutableArray *infoDictionaries = [NSMutableArray array];
if ([[NSBundle mainBundle] localizedInfoDictionary]) {
[infoDictionaries addObject:[[NSBundle mainBundle] localizedInfoDictionary]];
}
if ([[NSBundle mainBundle] infoDictionary]) {
[infoDictionaries addObject:[[NSBundle mainBundle] infoDictionary]];
}
for (NSDictionary *infoDictionary in infoDictionaries) {
for (NSString *appNameKey in appNameKeys) {
displayName = [infoDictionary objectForKey:appNameKey];
if (displayName != nil) {
break;
}
}
}
return displayName;
}
- (NSURL *)URLForRatingApp {
NSString *URLString = nil;
NSString *URLStringFromPreferences = [[NSUserDefaults standardUserDefaults] objectForKey:ATAppRatingReviewURLPreferenceKey];
if (URLStringFromPreferences == nil) {
#if TARGET_OS_IPHONE
NSString *osVersion = [[UIDevice currentDevice] systemVersion];
if ([ATUtilities versionString:osVersion isGreaterThanVersionString:@"6.0"] || [ATUtilities versionString:osVersion isEqualToVersionString:@"6.0"]) {
URLString = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/%@/app/id%@", [[NSLocale currentLocale] objectForKey: NSLocaleCountryCode], iTunesAppID];
} else {
URLString = [NSString stringWithFormat:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@", iTunesAppID];
}
#elif TARGET_OS_MAC
URLString = [NSString stringWithFormat:@"macappstore://itunes.apple.com/app/id%@?mt=12", iTunesAppID];
#endif
} else {
URLString = URLStringFromPreferences;
}
return [NSURL URLWithString:URLString];
}
- (BOOL)requirementsToShowDialogMet {
BOOL result = NO;
do { // once
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// Ratings are disabled, don't show dialog.
if ([[defaults objectForKey:ATAppRatingEnabledPreferenceKey] boolValue] == NO) {
break;
}
// Check to see if the user has rated the app.
NSNumber *rated = [defaults objectForKey:ATAppRatingFlowRatedAppKey];
if (rated != nil && [rated boolValue]) {
break;
}
// Check to see if the user has rejected rating this version.
NSNumber *rejected = [defaults objectForKey:ATAppRatingFlowDeclinedToRateThisVersionKey];
if (rejected != nil && [rejected boolValue]) {
break;
}
// Check to see if the user dislikes this version of the app.
NSNumber *dislikes = [defaults objectForKey:ATAppRatingFlowUserDislikesThisVersionKey];
if (dislikes != nil && [dislikes boolValue]) {
break;
}
// If we don't have the last version set, update it and don't show
// the dialog.
NSString *lastBundleVersion = [defaults objectForKey:ATAppRatingFlowLastUsedVersionKey];
if (lastBundleVersion == nil) {
[self updateVersionInfo];
break;
}
// If the user has been prompted already, make sure we're after the
// number of days for them to be re-prompted.
NSDate *lastPrompt = [defaults objectForKey:ATAppRatingFlowLastPromptDateKey];
if (lastPrompt != nil && self.daysBeforeRePrompting != 0) {
double nextPromptDouble = [lastPrompt timeIntervalSince1970] + 60*60*24*self.daysBeforeRePrompting;
if ([[NSDate date] timeIntervalSince1970] < nextPromptDouble) {
break;
}
}
ATAppRatingFlowPredicateInfo *info = [[ATAppRatingFlowPredicateInfo alloc] init];
info.firstUse = [defaults objectForKey:ATAppRatingFlowLastUsedVersionFirstUseDateKey];
info.significantEvents = [[defaults objectForKey:ATAppRatingFlowSignificantEventsCountKey] unsignedIntegerValue];
info.appUses = [[defaults objectForKey:ATAppRatingFlowUseCountKey] unsignedIntegerValue];
info.daysBeforePrompt = self.daysBeforePrompt;
info.significantEventsBeforePrompt = self.significantEventsBeforePrompt;
info.usesBeforePrompt = self.usesBeforePrompt;
NSPredicate *predicate = [ATAppRatingFlow_Private predicateForPromptLogic:[defaults objectForKey:ATAppRatingPromptLogicPreferenceKey] withPredicateInfo:info];
if (predicate) {
result = [ATAppRatingFlow_Private evaluatePredicate:predicate withPredicateInfo:info];
}
[info release], info = nil;
} while (NO);
return result;
}
- (BOOL)shouldShowDialog {
// No network connection, don't show dialog.
if ([[ATReachability sharedReachability] currentNetworkStatus] == ATNetworkNotReachable) {
return NO;
}
return [self requirementsToShowDialogMet];
}
- (BOOL)showDialogIfNecessary {
#if TARGET_OS_IPHONE
if ([self shouldShowDialog]) {
[self showEnjoymentDialog:self.viewController];
return YES;
} else if ([self rootViewControllerForCurrentWindow]) {
[self tryToShowDialogWaitingForReachability];
}
#elif TARGET_OS_MAC
if ([self shouldShowDialog]) {
[self showEnjoymentDialog:self];
} else {
[self tryToShowDialogWaitingForReachability];
}
#endif
return NO;
}
- (void)updateVersionInfo {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *currentBundleVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];
NSString *lastBundleVersion = [defaults objectForKey:ATAppRatingFlowLastUsedVersionKey];
if (lastBundleVersion == nil || ![lastBundleVersion isEqualToString:currentBundleVersion]) {
BOOL clearCounts = [(NSNumber *)[defaults objectForKey:ATAppRatingClearCountsOnUpgradePreferenceKey] boolValue];
if (clearCounts) {
// Clear the counters.
[defaults setObject:[NSNumber numberWithUnsignedInteger:0] forKey:ATAppRatingFlowUseCountKey];
[defaults setObject:[NSNumber numberWithUnsignedInteger:0] forKey:ATAppRatingFlowSignificantEventsCountKey];
[defaults setObject:[NSNumber numberWithBool:NO] forKey:ATAppRatingFlowRatedAppKey];
}
[defaults setObject:currentBundleVersion forKey:ATAppRatingFlowLastUsedVersionKey];
[defaults setObject:[NSDate date] forKey:ATAppRatingFlowLastUsedVersionFirstUseDateKey];
[defaults setObject:[NSNumber numberWithBool:NO] forKey:ATAppRatingFlowDeclinedToRateThisVersionKey];
[defaults setObject:[NSNumber numberWithBool:NO] forKey:ATAppRatingFlowUserDislikesThisVersionKey];
[defaults synchronize];
}
}
- (void)userDidUseApp {
if (lastUseOfApp != nil) {
NSTimeInterval interval = [lastUseOfApp timeIntervalSinceNow];
if (interval >= -kATAppAppUsageMinimumInterval) {
[self updateLastUseOfApp];
return;
}
}
[self updateLastUseOfApp];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSNumber *useCount = [defaults objectForKey:ATAppRatingFlowUseCountKey];
NSUInteger count = 0;
if (useCount != nil) {
count = [useCount unsignedIntegerValue];
}
count++;
[defaults setObject:[NSNumber numberWithUnsignedInteger:count] forKey:ATAppRatingFlowUseCountKey];
[defaults synchronize];
[self updateVersionInfo];
}
- (void)userDidSignificantEvent {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSNumber *eventCount = [defaults objectForKey:ATAppRatingFlowSignificantEventsCountKey];
NSUInteger count = 0;
if (eventCount != nil) {
count = [eventCount unsignedIntegerValue];
}
[defaults setObject:[NSNumber numberWithUnsignedInteger:count+1] forKey:ATAppRatingFlowSignificantEventsCountKey];
[defaults synchronize];
}
- (void)setRatingDialogWasShown {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSDate date] forKey:ATAppRatingFlowLastPromptDateKey];
[defaults synchronize];
}
- (void)setUserDislikesThisVersion {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithBool:YES] forKey:ATAppRatingFlowUserDislikesThisVersionKey];
[defaults synchronize];
}
- (void)setDeclinedToRateThisVersion {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithBool:YES] forKey:ATAppRatingFlowDeclinedToRateThisVersionKey];
[defaults synchronize];
}
- (void)setRatedApp {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithBool:YES] forKey:ATAppRatingFlowRatedAppKey];
[defaults synchronize];
}
- (void)logDefaults {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *keys = [NSArray arrayWithObjects:ATAppRatingFlowLastUsedVersionKey, ATAppRatingFlowLastUsedVersionFirstUseDateKey, ATAppRatingFlowDeclinedToRateThisVersionKey, ATAppRatingFlowUserDislikesThisVersionKey, ATAppRatingFlowLastPromptDateKey, ATAppRatingFlowUseCountKey, ATAppRatingFlowSignificantEventsCountKey, ATAppRatingFlowRatedAppKey, nil];
NSLog(@"-- BEGIN ATAppRatingFlow DEFAULTS --");
for (NSString *key in keys) {
NSLog(@"%@ == %@", key, [defaults objectForKey:key]);
}
NSLog(@"-- END ATAppRatingFlow DEFAULTS --");
}
#if TARGET_OS_IPHONE
- (void)appDidFinishLaunching:(NSNotification *)notification {
[self userDidUseApp];
}
- (void)appDidEnterBackground:(NSNotification *)notification {
[self updateLastUseOfApp];
}
- (void)appWillEnterForeground:(NSNotification *)notification {
[self userDidUseApp];
}
- (void)appWillResignActive:(NSNotification *)notification {
[self updateLastUseOfApp];
}
- (void)appWillEnterBackground:(NSNotification *)notification {
// We want to hide any dialogs here.
if (enjoymentDialog) {
[enjoymentDialog dismissWithClickedButtonIndex:3 animated:NO];
}
if (ratingDialog) {
[ratingDialog dismissWithClickedButtonIndex:3 animated:NO];
}
[self updateLastUseOfApp];
}
- (UIViewController *)rootViewControllerForCurrentWindow {
UIWindow *window = nil;
if (self.viewController && self.viewController.view && self.viewController.view.window) {
window = self.viewController.view.window;
} else {
for (UIWindow *tmpWindow in [[UIApplication sharedApplication] windows]) {
if ([[tmpWindow screen] isEqual:[UIScreen mainScreen]] && [tmpWindow isKeyWindow]) {
window = tmpWindow;
break;
}
}
}
if (window && [window respondsToSelector:@selector(rootViewController)]) {
UIViewController *vc = [window rootViewController];
if ([vc respondsToSelector:@selector(presentedViewController)] && [vc presentedViewController]) {
return [vc presentedViewController];
}
if ([vc respondsToSelector:@selector(modalViewController)] && [vc modalViewController]) {
return [vc modalViewController];
}
return vc;
} else {
return nil;
}
}
#endif
- (void)tryToShowDialogWaitingForReachability {
if (![[NSThread currentThread] isMainThread]) {
[self performSelectorOnMainThread:@selector(tryToShowDialogWaitingForReachability) withObject:nil waitUntilDone:NO];
return;
}
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
#if TARGET_OS_IPHONE
UIViewController *vc = [self rootViewControllerForCurrentWindow];
if (vc && [self requirementsToShowDialogMet]) {
// We can get a root view controller and we should be showing a dialog.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChangedAndPendingDialog:) name:ATReachabilityStatusChanged object:nil];
}
#elif TARGET_OS_MAC
if ([self requirementsToShowDialogMet]) {
// We should show a ratings dialog.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChangedAndPendingDialog:) name:ATReachabilityStatusChanged object:nil];
}
#endif
[pool release], pool = nil;
}
- (void)reachabilityChangedAndPendingDialog:(NSNotification *)notification {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[[NSNotificationCenter defaultCenter] removeObserver:self name:ATReachabilityStatusChanged object:nil];
#if TARGET_OS_IPHONE
UIViewController *vc = [self rootViewControllerForCurrentWindow];
if (vc && [self requirementsToShowDialogMet]) {
if ([[ATReachability sharedReachability] currentNetworkStatus] == ATNetworkNotReachable) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChangedAndPendingDialog:) name:ATReachabilityStatusChanged object:nil];
} else {
[self showEnjoymentDialog:vc];
}
}
#elif TARGET_OS_MAC
if ([self requirementsToShowDialogMet]) {
if ([[ATReachability sharedReachability] currentNetworkStatus] == ATNetworkNotReachable) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChangedAndPendingDialog:) name:ATReachabilityStatusChanged object:nil];
} else {
[self showEnjoymentDialog:self];
}
}
#endif
[pool release], pool = nil;
}
- (void)preferencesChanged:(NSNotification *)notification {
[self loadPreferences];
}
- (void)loadPreferences {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
daysBeforePrompt = [(NSNumber *)[defaults objectForKey:ATAppRatingDaysBeforePromptPreferenceKey] unsignedIntegerValue];
usesBeforePrompt = [(NSNumber *)[defaults objectForKey:ATAppRatingUsesBeforePromptPreferenceKey] unsignedIntegerValue];
significantEventsBeforePrompt = [(NSNumber *)[defaults objectForKey:ATAppRatingSignificantEventsBeforePromptPreferenceKey] unsignedIntegerValue];
daysBeforeRePrompting = [(NSNumber *)[defaults objectForKey:ATAppRatingDaysBetweenPromptsPreferenceKey] unsignedIntegerValue];
}
@end