-
Notifications
You must be signed in to change notification settings - Fork 117
/
UAAppReviewManager.m
1375 lines (1123 loc) · 53.5 KB
/
UAAppReviewManager.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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// UAAppReviewManager.m
//
// Created by Matt Coneybeare on 9/8/13.
// http://matt.coneybeare.me
// Copyright (c) 2013 Urban Apps. All rights reserved.
// http://urbanapps.com
//
#import "UAAppReviewManager.h"
#import <SystemConfiguration/SCNetworkReachability.h>
#include <netinet/in.h>
#if ! __has_feature(objc_arc)
#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
#endif
#define UAAppReviewManagerDebugLog( s, ... ) if (self.debugEnabled) { NSLog(@"[UAAppReviewManager] %@", [NSString stringWithFormat:(s), ##__VA_ARGS__]); }
#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
#define UAAppReviewManagerSystemVersionEqualTo(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define UAAppReviewManagerSystemVersionLessThan(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define UAAppReviewManagerSystemVersionLessThanOrEqualTo(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
#define UAAppReviewManagerSystemVersionGreaterThan(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define UAAppReviewManagerSystemVersionGreaterThanOrEqualTo(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#endif
// For conversion purposes, we keep these here to help people migrate from Appirater to UAAppReviewManager
// The keys used by UAAppReviewManager are settable by the developer
static NSString * const kAppiraterFirstUseDate = @"kAppiraterFirstUseDate";
static NSString * const kAppiraterUseCount = @"kAppiraterUseCount";
static NSString * const kAppiraterSignificantEventCount = @"kAppiraterSignificantEventCount";
static NSString * const kAppiraterCurrentVersion = @"kAppiraterCurrentVersion";
static NSString * const kAppiraterRatedCurrentVersion = @"kAppiraterRatedCurrentVersion";
static NSString * const kAppiraterRatedAnyVersion = @"kAppiraterRatedAnyVersion";
static NSString * const kAppiraterDeclinedToRate = @"kAppiraterDeclinedToRate";
static NSString * const kAppiraterReminderRequestDate = @"kAppiraterReminderRequestDate";
// The templates used for opening the app store directly
#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
static NSString * const reviewURLTemplate = @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=APP_ID&at=AFFILIATE_CODE&ct=AFFILIATE_CAMPAIGN_CODE";
static NSString * const reviewURLTemplateiOS7 = @"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&onlyLatestVersion=true&pageNumber=0&sortOrdering=1&id=APP_ID&at=AFFILIATE_CODE&ct=AFFILIATE_CAMPAIGN_CODE";
#else
static NSString * const reviewURLTemplate = @"macappstore://itunes.apple.com/us/app/thumbs/idAPP_ID?ls=1&mt=12&at=AFFILIATE_CODE&ct=AFFILIATE_CAMPAIGN_CODE";
#endif
@interface UAAppReviewManager ()
// Review Alert Properties
@property (nonatomic, strong) NSString *appName;
@property (nonatomic, strong) NSString *reviewTitle;
@property (nonatomic, strong) NSString *reviewMessage;
@property (nonatomic, strong) NSString *cancelButtonTitle;
@property (nonatomic, strong) NSString *rateButtonTitle;
@property (nonatomic, strong) NSString *remindButtonTitle;
// Tracking Logic / Configuration
@property (nonatomic, strong) NSString *appID;
@property (nonatomic, assign) NSUInteger daysUntilPrompt;
@property (nonatomic, assign) NSUInteger usesUntilPrompt;
@property (nonatomic, assign) NSUInteger significantEventsUntilPrompt;
@property (nonatomic, assign) NSUInteger daysBeforeReminding;
@property (nonatomic, assign) BOOL tracksNewVersions;
@property (nonatomic, assign) BOOL shouldPromptIfRated;
@property (nonatomic, assign) BOOL useMainAppBundleForLocalizations;
@property (nonatomic, strong) NSString *affiliateCode;
@property (nonatomic, strong) NSString *affiliateCampaignCode;
@property (nonatomic, assign) BOOL debugEnabled;
#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
@property (nonatomic, assign) BOOL usesAnimation;
@property (nonatomic, assign) BOOL opensInStoreKit;
#endif
// Tracking Keys
@property (nonatomic, strong) NSString *appReviewManagerKeyFirstUseDate;
@property (nonatomic, strong) NSString *appReviewManagerKeyUseCount;
@property (nonatomic, strong) NSString *appReviewManagerKeySignificantEventCount;
@property (nonatomic, strong) NSString *appReviewManagerKeyCurrentVersion;
@property (nonatomic, strong) NSString *appReviewManagerKeyRatedCurrentVersion;
@property (nonatomic, strong) NSString *appReviewManagerKeyDeclinedToRate;
@property (nonatomic, strong) NSString *appReviewManagerKeyReminderRequestDate;
@property (nonatomic, strong) NSString *appReviewManagerKeyPreviousVersion;
@property (nonatomic, strong) NSString *appReviewManagerKeyPreviousVersionRated;
@property (nonatomic, strong) NSString *appReviewManagerKeyPreviousVersionDeclinedToRate;
@property (nonatomic, strong) NSString *appReviewManagerKeyRatedAnyVersion;
@property (nonatomic, strong) NSString *appReviewManagerKeyAppiraterMigrationCompleted;
@property (nonatomic, strong) NSString *keyPrefix;
@property (nonatomic, strong) NSObject<UAAppReviewManagerDefaultsObject> *userDefaultsObject;
// Blocks
@property (nonatomic, copy) UAAppReviewManagerBlock didDisplayAlertBlock;
@property (nonatomic, copy) UAAppReviewManagerBlock didDeclineToRateBlock;
@property (nonatomic, copy) UAAppReviewManagerBlock didOptToRateBlock;
@property (nonatomic, copy) UAAppReviewManagerBlock didOptToRemindLaterBlock;
#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
@property (nonatomic, copy) UAAppReviewManagerAnimateBlock willPresentModalViewBlock;
@property (nonatomic, copy) UAAppReviewManagerAnimateBlock didDismissModalViewBlock;
#endif
@property (nonatomic, copy) UAAppReviewManagerShouldPromptBlock shouldPromptBlock;
@property (nonatomic, copy) UAAppReviewManagerShouldIncrementBlock shouldIncrementUseCountBlock;
// State ivars
@property (nonatomic, assign) BOOL modalPanelOpen;
#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
@property (nonatomic, assign) UIStatusBarStyle currentStatusBarStyle;
#endif
@end
@implementation UAAppReviewManager
#pragma mark - PUBLIC Class Convenience Methods
+ (NSString *)appName {
return [[UAAppReviewManager defaultManager] appName];
}
+ (void)setAppName:(NSString *)appName {
[[UAAppReviewManager defaultManager] setAppName:appName];
}
+ (NSString *)reviewTitle {
return [[UAAppReviewManager defaultManager] reviewTitle];
}
+ (void)setReviewTitle:(NSString *)reviewTitle {
[[UAAppReviewManager defaultManager] setReviewTitle:reviewTitle];
}
+ (NSString *)reviewMessage {
return [[UAAppReviewManager defaultManager] reviewMessage];
}
+ (void)setReviewMessage:(NSString *)reviewMessage {
[[UAAppReviewManager defaultManager] setReviewMessage:reviewMessage];
}
+ (NSString *)cancelButtonTitle {
return [[UAAppReviewManager defaultManager] cancelButtonTitle];
}
+ (void)setCancelButtonTitle:(NSString *)cancelButtonTitle {
[[UAAppReviewManager defaultManager] setCancelButtonTitle:cancelButtonTitle];
}
+ (NSString *)rateButtonTitle {
return [[UAAppReviewManager defaultManager] rateButtonTitle];
}
+ (void)setRateButtonTitle:(NSString *)rateButtonTitle {
[[UAAppReviewManager defaultManager] setRateButtonTitle:rateButtonTitle];
}
+ (NSString *)remindButtonTitle {
return [[UAAppReviewManager defaultManager] remindButtonTitle];
}
+ (void)setRemindButtonTitle:(NSString *)remindButtonTitle {
[[UAAppReviewManager defaultManager] setRemindButtonTitle:remindButtonTitle];
}
+ (NSString *)keyForUAAppReviewManagerKeyType:(UAAppReviewManagerKeyType)keyType {
return [[UAAppReviewManager defaultManager] keyForUAAppReviewManagerKeyType:keyType];
}
+ (void)setKey:(NSString *)key forUAAppReviewManagerKeyType:(UAAppReviewManagerKeyType)keyType {
[[UAAppReviewManager defaultManager] setKey:key forUAAppReviewManagerKeyType:keyType];
}
+ (NSString *)keyPrefix {
return [[UAAppReviewManager defaultManager] keyPrefix];
}
+ (void)setKeyPrefix:(NSString *)keyPrefix {
[[UAAppReviewManager defaultManager] setKeyPrefix:keyPrefix];
}
+ (NSObject<UAAppReviewManagerDefaultsObject> *)userDefaultsObject {
return [[UAAppReviewManager defaultManager] userDefaultsObject];
}
+ (void)setUserDefaultsObject:(NSObject<UAAppReviewManagerDefaultsObject> *)userDefaultsObject {
[[UAAppReviewManager defaultManager] setUserDefaultsObject:userDefaultsObject];
}
+ (NSString *)appID {
return [[UAAppReviewManager defaultManager] appID];
}
+ (void)setAppID:(NSString *)appID {
[[UAAppReviewManager defaultManager] setAppID:appID];
}
+ (NSUInteger)daysUntilPrompt {
return [[UAAppReviewManager defaultManager] daysUntilPrompt];
}
+ (void)setDaysUntilPrompt:(NSUInteger)daysUntilPrompt {
[[UAAppReviewManager defaultManager] setDaysUntilPrompt:daysUntilPrompt];
}
+ (NSUInteger)usesUntilPrompt {
return [[UAAppReviewManager defaultManager] usesUntilPrompt];
}
+ (void)setUsesUntilPrompt:(NSUInteger)usesUntilPrompt {
[[UAAppReviewManager defaultManager] setUsesUntilPrompt:usesUntilPrompt];
}
+ (NSUInteger)significantEventsUntilPrompt {
return [[UAAppReviewManager defaultManager] significantEventsUntilPrompt];
}
+ (void)setSignificantEventsUntilPrompt:(NSUInteger)significantEventsUntilPrompt {
[[UAAppReviewManager defaultManager] setSignificantEventsUntilPrompt:significantEventsUntilPrompt];
}
+ (NSUInteger)daysBeforeReminding {
return [[UAAppReviewManager defaultManager] daysBeforeReminding];
}
+ (void)setDaysBeforeReminding:(NSUInteger)daysBeforeReminding {
[[UAAppReviewManager defaultManager] setDaysBeforeReminding:daysBeforeReminding];
}
+ (BOOL)tracksNewVersions {
return [[UAAppReviewManager defaultManager] tracksNewVersions];
}
+ (void)setTracksNewVersions:(BOOL)tracksNewVersions {
[[UAAppReviewManager defaultManager] setTracksNewVersions:tracksNewVersions];
}
+ (BOOL)shouldPromptIfRated {
return [[UAAppReviewManager defaultManager] shouldPromptIfRated];
}
+ (void)setShouldPromptIfRated:(BOOL)shouldPromptIfRated {
[[UAAppReviewManager defaultManager] setShouldPromptIfRated:shouldPromptIfRated];
}
+ (BOOL)useMainAppBundleForLocalizations {
return [[UAAppReviewManager defaultManager] useMainAppBundleForLocalizations];
}
+ (void)setUseMainAppBundleForLocalizations:(BOOL)useMainAppBundleForLocalizations {
[[UAAppReviewManager defaultManager] setUseMainAppBundleForLocalizations:useMainAppBundleForLocalizations];
}
+ (NSString *)affiliateCode {
return [[UAAppReviewManager defaultManager] affiliateCode];
}
+ (void)setAffiliateCode:(NSString*)affiliateCode {
[[UAAppReviewManager defaultManager] setAffiliateCode:affiliateCode];
}
+ (NSString *)affiliateCampaignCode {
return [[UAAppReviewManager defaultManager] affiliateCampaignCode];
}
+ (void)setAffiliateCampaignCode:(NSString*)affiliateCampaignCode {
[[UAAppReviewManager defaultManager] setAffiliateCampaignCode:affiliateCampaignCode];
}
+ (BOOL)debug {
return [[UAAppReviewManager defaultManager] debugEnabled];
}
+ (void)setDebug:(BOOL)debug {
#ifdef DEBUG
[[UAAppReviewManager defaultManager] setDebugEnabled:debug];
#endif
}
#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
+ (BOOL)usesAnimation {
return [[UAAppReviewManager defaultManager] usesAnimation];
}
+ (void)setUsesAnimation:(BOOL)usesAnimation {
[[UAAppReviewManager defaultManager] setUsesAnimation:usesAnimation];
}
+ (BOOL)opensInStoreKit {
return [[UAAppReviewManager defaultManager] opensInStoreKit];
}
+ (void)setOpensInStoreKit:(BOOL)opensInStoreKit {
[[UAAppReviewManager defaultManager] setOpensInStoreKit:opensInStoreKit];
}
#endif
+ (void)userDidSignificantEvent:(BOOL)canPromptForRating {
[[UAAppReviewManager defaultManager] userDidSignificantEvent:canPromptForRating];
}
+ (void)userDidSignificantEventWithShouldPromptBlock:(UAAppReviewManagerShouldPromptBlock)shouldPromptBlock {
[[UAAppReviewManager defaultManager] userDidSignificantEventWithShouldPromptBlock:shouldPromptBlock];
}
+ (void)showPrompt {
[[UAAppReviewManager defaultManager] showPrompt];
}
+ (void)showPromptIfNecessary {
[[UAAppReviewManager defaultManager] showPromptIfNecessary:YES];
}
+ (void)showPromptWithShouldPromptBlock:(UAAppReviewManagerShouldPromptBlock)shouldPromptBlock {
[[UAAppReviewManager defaultManager] showPromptWithShouldPromptBlock:shouldPromptBlock];
}
+ (NSString *)reviewURLString {
return [[UAAppReviewManager defaultManager] reviewURLString];
}
+ (BOOL)canRateApp {
return [[UAAppReviewManager defaultManager] ratingConditionsHaveBeenMet];
}
+ (void)dontRate {
[[UAAppReviewManager defaultManager] dontRate];
}
+ (void)remindMeLater {
[[UAAppReviewManager defaultManager] remindMeLater];
}
+ (void)rateApp {
[[UAAppReviewManager defaultManager] rateApp];
}
#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
+ (void)closeModalPanel {
[[UAAppReviewManager defaultManager] closeModalPanel];
}
#endif
+ (void)setOnDidDisplayAlert:(UAAppReviewManagerBlock)didDisplayAlertBlock {
[[UAAppReviewManager defaultManager] setDidDisplayAlertBlock:didDisplayAlertBlock];
}
+ (void)setOnDeclineToRate:(UAAppReviewManagerBlock)didDeclineToRateBlock {
[[UAAppReviewManager defaultManager] setDidDeclineToRateBlock:didDeclineToRateBlock];
}
+ (void)setOnDidOptToRate:(UAAppReviewManagerBlock)didOptToRateBlock {
[[UAAppReviewManager defaultManager] setDidOptToRateBlock:didOptToRateBlock];
}
+ (void)setOnDidOptToRemindLater:(UAAppReviewManagerBlock)didOptToRemindLaterBlock {
[[UAAppReviewManager defaultManager] setDidOptToRemindLaterBlock:didOptToRemindLaterBlock];
}
#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
+ (void)setOnWillPresentModalView:(UAAppReviewManagerAnimateBlock)willPresentModalViewBlock {
[[UAAppReviewManager defaultManager] setWillPresentModalViewBlock:willPresentModalViewBlock];
}
+ (void)setOnDidDismissModalView:(UAAppReviewManagerAnimateBlock)didDismissModalViewBlock {
[[UAAppReviewManager defaultManager] setDidDismissModalViewBlock:didDismissModalViewBlock];
}
#endif
+ (void)setShouldPromptBlock:(UAAppReviewManagerShouldPromptBlock)shouldPromptBlock {
[[UAAppReviewManager defaultManager] setShouldPromptBlock:shouldPromptBlock];
}
+ (void)setShouldIncrementUseCountBlock:(UAAppReviewManagerShouldIncrementBlock)shouldIncrementUseCountBlock {
[[UAAppReviewManager defaultManager] setShouldIncrementUseCountBlock:shouldIncrementUseCountBlock];
}
#pragma mark - PUBLIC Class Convenience Methods (backwards compatibility)
+ (void)appLaunched:(BOOL)canPromptForRating {
[[UAAppReviewManager defaultManager] showPromptIfNecessary:canPromptForRating];
}
+ (void)appLaunchedWithShouldPromptBlock:(UAAppReviewManagerShouldPromptBlock)shouldPromptBlock {
[[UAAppReviewManager defaultManager] showPromptWithShouldPromptBlock:shouldPromptBlock];
}
+ (void)appEnteredForeground:(BOOL)canPromptForRating {
[[UAAppReviewManager defaultManager] showPromptIfNecessary:canPromptForRating];
}
+ (void)appEnteredForegroundWithShouldPromptBlock:(UAAppReviewManagerShouldPromptBlock)shouldPromptBlock {
[[UAAppReviewManager defaultManager] showPromptWithShouldPromptBlock:shouldPromptBlock];
}
+ (void)setAppId:(NSString*)appId {
[UAAppReviewManager setAppID:appId];
}
+ (void)setTimeBeforeReminding:(double)value {
[UAAppReviewManager setDaysBeforeReminding:(NSUInteger)value];
}
+ (void)setAlwaysUseMainBundle:(BOOL)useMainBundle {
[UAAppReviewManager setUseMainAppBundleForLocalizations:useMainBundle];
}
+ (void)appLaunched {
[UAAppReviewManager appLaunched:NO];
}
+ (void)setDelegate:(id)delegate {
// No analagous method
}
#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
+ (void)setOpenInAppStore:(BOOL)openInAppStore {
[UAAppReviewManager setOpensInStoreKit:!openInAppStore];
}
#endif
#pragma mark -
#pragma mark - PRIVATE Review Alert Property Accessors
- (void)setAppID:(NSString *)appID {
if ([appID length]) {
_appID = appID;
if ([_affiliateCampaignCode isEqualToString:self.defaultAffiliateCode]) {
_affiliateCampaignCode = [_affiliateCampaignCode stringByAppendingFormat:@"-%@", _appID];
}
}
}
- (void)setAffiliateCode:(NSString *)affiliateCode {
if ([affiliateCode length])
_affiliateCode = affiliateCode;
}
- (void)setAffiliateCampaignCode:(NSString *)affiliateCampaignCode {
if ([affiliateCampaignCode length])
_affiliateCampaignCode = affiliateCampaignCode;
}
- (NSString *)appName {
if (!_appName) {
// Check for a localized version of the CFBundleDisplayName
NSString *appName = [[NSBundle mainBundle] localizedInfoDictionary][@"CFBundleDisplayName"];
if (!appName)
appName = [[NSBundle mainBundle] localizedInfoDictionary][(id) kCFBundleNameKey];
if (!appName)
appName = [[NSBundle mainBundle] infoDictionary][@"CFBundleDisplayName"];
if (!appName)
appName = [[NSBundle mainBundle] infoDictionary][(id) kCFBundleNameKey];
self.appName = appName;
}
return _appName;
}
- (NSString *)reviewTitle {
if (!_reviewTitle) {
// Check for a localized version of the default title
NSString *reviewTitleTemplate = NSLocalizedStringFromTableInBundle(@"Rate %@",
@"UAAppReviewManagerLocalizable",
[self bundle],
nil);
self.reviewTitle = [NSString stringWithFormat:reviewTitleTemplate, [self appName]];
}
return _reviewTitle;
}
- (NSString *)reviewMessage {
if (!_reviewMessage) {
// Check for a localized version of the default message
NSString *reviewMessageTemplate = 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!",
@"UAAppReviewManagerLocalizable",
[self bundle],
nil);
self.reviewMessage = [NSString stringWithFormat:reviewMessageTemplate, [self appName]];
}
return _reviewMessage;
}
- (NSString *)cancelButtonTitle {
if (!_cancelButtonTitle) {
// Check for a localized version of the default title
self.cancelButtonTitle = NSLocalizedStringFromTableInBundle(@"No, Thanks", @"UAAppReviewManagerLocalizable", [self bundle], nil);
}
return _cancelButtonTitle;
}
- (NSString *)rateButtonTitle {
if (!_rateButtonTitle) {
// Check for a localized version of the default title
NSString *rateTitleTemplate = NSLocalizedStringFromTableInBundle(@"Rate %@", @"UAAppReviewManagerLocalizable", [self bundle], nil);
self.rateButtonTitle = [NSString stringWithFormat:rateTitleTemplate, [self appName]];
}
return _rateButtonTitle;
}
- (NSString *)remindButtonTitle {
if (self.daysBeforeReminding == 0) {
return nil; //if reminders are disabled, return a nil title to supress the button
}
if (!_remindButtonTitle) {
// Check for a localized version of the default title
self.remindButtonTitle = NSLocalizedStringFromTableInBundle(@"Remind me later", @"UAAppReviewManagerLocalizable", [self bundle], nil);
}
return _remindButtonTitle;
}
- (BOOL)showsRemindButton {
return self.remindButtonTitle.length != 0;
}
#pragma mark - PRIVATE Tracking Key Accessors
// Tracking Keys
- (NSString *)appReviewManagerKeyFirstUseDate {
if (!_appReviewManagerKeyFirstUseDate) {
// Provide a sensible default
self.appReviewManagerKeyFirstUseDate = @"UAAppReviewManagerKeyFirstUseDate";
}
return _appReviewManagerKeyFirstUseDate;
}
- (NSString *)appReviewManagerKeyUseCount {
if (!_appReviewManagerKeyUseCount) {
// Provide a sensible default
self.appReviewManagerKeyUseCount = @"UAAppReviewManagerKeyUseCount";
}
return _appReviewManagerKeyUseCount;
}
- (NSString *)appReviewManagerKeySignificantEventCount {
if (!_appReviewManagerKeySignificantEventCount) {
// Provide a sensible default
self.appReviewManagerKeySignificantEventCount = @"UAAppReviewManagerKeySignificantEventCount";
}
return _appReviewManagerKeySignificantEventCount;
}
- (NSString *)appReviewManagerKeyCurrentVersion {
if (!_appReviewManagerKeyCurrentVersion) {
// Provide a sensible default
self.appReviewManagerKeyCurrentVersion = @"UAAppReviewManagerKeyCurrentVersion";
}
return _appReviewManagerKeyCurrentVersion;
}
- (NSString *)appReviewManagerKeyRatedCurrentVersion {
if (!_appReviewManagerKeyRatedCurrentVersion) {
// Provide a sensible default
self.appReviewManagerKeyRatedCurrentVersion = @"UAAppReviewManagerKeyRatedCurrentVersion";
}
return _appReviewManagerKeyRatedCurrentVersion;
}
- (NSString *)appReviewManagerKeyDeclinedToRate {
if (!_appReviewManagerKeyDeclinedToRate) {
// Provide a sensible default
self.appReviewManagerKeyDeclinedToRate = @"UAAppReviewManagerKeyDeclinedToRate";
}
return _appReviewManagerKeyDeclinedToRate;
}
- (NSString *)appReviewManagerKeyReminderRequestDate {
if (!_appReviewManagerKeyReminderRequestDate) {
// Provide a sensible default
self.appReviewManagerKeyReminderRequestDate = @"UAAppReviewManagerKeyReminderRequestDate";
}
return _appReviewManagerKeyReminderRequestDate;
}
- (NSString *)appReviewManagerKeyPreviousVersion {
if (!_appReviewManagerKeyPreviousVersion) {
// Provide a sensible default
self.appReviewManagerKeyPreviousVersion = @"UAAppReviewManagerKeyPreviousVersion";
}
return _appReviewManagerKeyPreviousVersion;
}
- (NSString *)appReviewManagerKeyPreviousVersionRated {
if (!_appReviewManagerKeyPreviousVersionRated) {
// Provide a sensible default
self.appReviewManagerKeyPreviousVersionRated = @"UAAppReviewManagerKeyPreviousVersionRated";
}
return _appReviewManagerKeyPreviousVersionRated;
}
- (NSString *)appReviewManagerKeyPreviousVersionDeclinedToRate {
if (!_appReviewManagerKeyPreviousVersionDeclinedToRate) {
// Provide a sensible default
self.appReviewManagerKeyPreviousVersionDeclinedToRate = @"UAAppReviewManagerKeyPreviousVersionDeclinedToRate";
}
return _appReviewManagerKeyPreviousVersionDeclinedToRate;
}
- (NSString *)appReviewManagerKeyRatedAnyVersion {
if (!_appReviewManagerKeyRatedAnyVersion) {
// Provide a sensible default
self.appReviewManagerKeyRatedAnyVersion = @"UAAppReviewManagerKeyRatedAnyVersion";
}
return _appReviewManagerKeyRatedAnyVersion;
}
- (NSString *)appReviewManagerKeyAppiraterMigrationCompleted {
if (!_appReviewManagerKeyAppiraterMigrationCompleted) {
// Provide a sensible default
self.appReviewManagerKeyAppiraterMigrationCompleted = @"UAAppReviewManagerKeyAppiraterMigrationCompleted";
}
return _appReviewManagerKeyAppiraterMigrationCompleted;
}
#pragma mark - PRIVATE Methods
- (void)userDidSignificantEvent:(BOOL)canPromptForRating {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
[self incrementSignificantEventAndRate:canPromptForRating];
});
}
- (void)userDidSignificantEventWithShouldPromptBlock:(UAAppReviewManagerShouldPromptBlock)shouldPromptBlock {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
[self incrementSignificantEventWithShouldPromptBlock:shouldPromptBlock];
});
}
#pragma mark - PRIVATE Rating Helpers
- (void)incrementAndRate:(BOOL)canPromptForRating {
[self migrateAppiraterKeysIfNecessary];
[self incrementUseCount];
[self showPromptIfNecessary:canPromptForRating];
}
- (void)incrementAndRateWithShouldPromptBlock:(UAAppReviewManagerShouldPromptBlock)shouldPromptBlock {
[self migrateAppiraterKeysIfNecessary];
[self incrementUseCount];
[self showPromptWithShouldPromptBlock:shouldPromptBlock];
}
- (void)incrementSignificantEventAndRate:(BOOL)canPromptForRating {
[self migrateAppiraterKeysIfNecessary];
[self incrementSignificantEventCount];
[self showPromptIfNecessary:canPromptForRating];
}
- (void)incrementSignificantEventWithShouldPromptBlock:(UAAppReviewManagerShouldPromptBlock)shouldPromptBlock {
[self migrateAppiraterKeysIfNecessary];
[self incrementSignificantEventCount];
[self showPromptWithShouldPromptBlock:shouldPromptBlock];
}
- (void)incrementUseCount {
if (self.shouldIncrementUseCountBlock == nil || self.shouldIncrementUseCountBlock())
{
[self _incrementCountForKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyUseCount]];
}
}
- (void)incrementSignificantEventCount {
[self _incrementCountForKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeySignificantEventCount]];
}
- (void)_incrementCountForKey:(NSString *)incrementKey {
// App's version. Not settable as the other ivars because that would be crazy.
NSString *currentVersion = [[NSBundle mainBundle] infoDictionary][(NSString *) kCFBundleVersionKey];
// Get the version number that we've been tracking thus far
NSString *currentVersionKey = [self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyCurrentVersion];
NSString *trackingVersion = [self.userDefaultsObject objectForKey:currentVersionKey];
// New install, or changed keys
if (!trackingVersion || ![trackingVersion isKindOfClass:[NSString class]]) {
trackingVersion = currentVersion;
[self.userDefaultsObject setObject:currentVersion forKey:currentVersionKey];
}
UAAppReviewManagerDebugLog(@"Tracking version: %@", trackingVersion);
if ([trackingVersion isEqualToString:currentVersion]) {
// Check if the first use date has been set. if not, set it.
NSString *firstUseDateKey = [self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyFirstUseDate];
NSTimeInterval timeInterval = [[self.userDefaultsObject objectForKey:firstUseDateKey] doubleValue];
if (0 == timeInterval) {
timeInterval = [[NSDate date] timeIntervalSince1970];
[self.userDefaultsObject setObject:@(timeInterval) forKey:firstUseDateKey];
}
// Increment the key's count
NSInteger incrementKeyCount = [[self.userDefaultsObject objectForKey:incrementKey] integerValue];
incrementKeyCount++;
[self.userDefaultsObject setObject:@(incrementKeyCount) forKey:incrementKey];
UAAppReviewManagerDebugLog(@"%@ count: %ld", incrementKey, (long)incrementKeyCount);
} else if (self.tracksNewVersions) {
// it's a new version of the app, so restart tracking
[self.userDefaultsObject setObject:trackingVersion
forKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyPreviousVersion]];
[self.userDefaultsObject setObject:[self.userDefaultsObject objectForKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyRatedCurrentVersion]]
forKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyPreviousVersionRated]];
BOOL declinedToRate = ([self userHasDeclinedToRate] || [self userHasPreviouslyDeclinedToRate]);
[self.userDefaultsObject setObject:@(declinedToRate)
forKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyPreviousVersionDeclinedToRate]];
[self.userDefaultsObject setObject:currentVersion forKey:currentVersionKey];
[self.userDefaultsObject setObject:@([[NSDate date] timeIntervalSince1970]) forKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyFirstUseDate]];
[self.userDefaultsObject setObject:@1 forKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyUseCount]];
[self.userDefaultsObject setObject:@0 forKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeySignificantEventCount]];
[self.userDefaultsObject setObject:@NO forKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyRatedCurrentVersion]];
[self.userDefaultsObject setObject:@NO forKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyDeclinedToRate]];
[self.userDefaultsObject setObject:@0.0 forKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyReminderRequestDate]];
}
[self.userDefaultsObject synchronize];
}
- (void)showPromptIfNecessary:(BOOL)canPromptForRating {
if (canPromptForRating && [self ratingConditionsHaveBeenMet] && [self connectedToNetwork]) {
__block BOOL shouldPrompt = YES;
if (self.shouldPromptBlock) {
if ([NSThread isMainThread]) {
shouldPrompt = self.shouldPromptBlock([self trackingInfo]);
} else {
dispatch_sync(dispatch_get_main_queue(), ^{
shouldPrompt = self.shouldPromptBlock([self trackingInfo]);
});
}
}
if (shouldPrompt) {
dispatch_async(dispatch_get_main_queue(), ^{
[self showRatingAlert];
});
}
}
}
- (void)showPromptWithShouldPromptBlock:(UAAppReviewManagerShouldPromptBlock)shouldPromptBlock {
__block BOOL shouldPrompt = NO;
if (shouldPromptBlock) {
if ([NSThread isMainThread]) {
shouldPrompt = shouldPromptBlock([self trackingInfo]);
} else {
dispatch_sync(dispatch_get_main_queue(), ^{
shouldPrompt = shouldPromptBlock([self trackingInfo]);
});
}
}
if (shouldPrompt) {
dispatch_async(dispatch_get_main_queue(), ^{
[self showRatingAlert];
});
}
}
- (void)showPrompt {
if ((self.appID && [self connectedToNetwork] && ![self userHasDeclinedToRate] && ![self userHasRatedCurrentVersion]) || self.debugEnabled) {
[self showRatingAlert];
}
}
- (BOOL)ratingConditionsHaveBeenMet {
if (self.debugEnabled)
return YES;
if (!self.appID)
return NO;
NSDate *dateOfFirstLaunch = [NSDate dateWithTimeIntervalSince1970:[[self.userDefaultsObject objectForKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyFirstUseDate]] doubleValue]];
NSTimeInterval timeSinceFirstLaunch = [[NSDate date] timeIntervalSinceDate:dateOfFirstLaunch];
NSTimeInterval timeUntilRate = 60 * 60 * 24 * self.daysUntilPrompt;
if (timeSinceFirstLaunch < timeUntilRate)
return NO;
// check if the app has been used enough
NSInteger useCount = [[self.userDefaultsObject objectForKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyUseCount]] integerValue];
if (useCount <= self.usesUntilPrompt)
return NO;
// check if the user has done enough significant events
NSInteger significantEventCount = [[self.userDefaultsObject objectForKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeySignificantEventCount]] integerValue];
if (significantEventCount < self.significantEventsUntilPrompt)
return NO;
// has the user previously declined to rate this version of the app?
if ([self userHasDeclinedToRate])
return NO;
// has the user already rated the app?
if ([self userHasRatedCurrentVersion])
return NO;
// if the user wanted to be reminded later, has enough time passed?
NSDate *reminderRequestDate = [NSDate dateWithTimeIntervalSince1970:[[self.userDefaultsObject objectForKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyReminderRequestDate]] doubleValue]];
NSTimeInterval timeSinceReminderRequest = [[NSDate date] timeIntervalSinceDate:reminderRequestDate];
NSTimeInterval timeUntilReminder = 60 * 60 * 24 * self.daysBeforeReminding;
if (timeSinceReminderRequest < timeUntilReminder)
return NO;
// if we have a global set to not show if the end-user has already rated once, and the developer has not opted out of displaying on minor updates
if (!self.shouldPromptIfRated && [[self.userDefaultsObject objectForKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyRatedAnyVersion]] boolValue])
return NO;
return YES;
}
- (BOOL)userHasDeclinedToRate {
return [[self.userDefaultsObject objectForKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyDeclinedToRate]] boolValue];
}
- (BOOL)userHasPreviouslyDeclinedToRate {
return [[self.userDefaultsObject objectForKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyPreviousVersionDeclinedToRate]] boolValue];
}
- (BOOL)userHasRatedCurrentVersion {
return [[self.userDefaultsObject objectForKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyRatedCurrentVersion]] boolValue];
}
#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
- (void)showRatingAlert {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:self.reviewTitle
message:self.reviewMessage
delegate:self
cancelButtonTitle:self.cancelButtonTitle
otherButtonTitles:(self.showsRemindButton ? self.remindButtonTitle : self.rateButtonTitle), // If we have a remind button, show it first. Otherwise show the rate button
(self.showsRemindButton ? self.rateButtonTitle : nil), // If we have a remind button, show the rate button next. Otherwise stop adding buttons.
nil];
alertView.cancelButtonIndex = -1;
self.ratingAlert = alertView;
[alertView show];
if (self.didDisplayAlertBlock)
self.didDisplayAlertBlock();
}
#else
- (void)showRatingAlert {
NSAlert *alert = [NSAlert new];
alert.messageText = self.reviewTitle;
alert.informativeText = self.reviewMessage;
[alert addButtonWithTitle:self.rateButtonTitle];
[alert addButtonWithTitle:self.remindButtonTitle];
[alert addButtonWithTitle:self.cancelButtonTitle];
self.ratingAlert = alert;
NSWindow *window = [[NSApplication sharedApplication] keyWindow];
if (window) {
// TODO: Deprecated function
[alert beginSheetModalForWindow:[[NSApplication sharedApplication] keyWindow]
modalDelegate:self
didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
contextInfo:nil];
} else {
NSInteger returnCode = [alert runModal];
[self handleNSAlertReturnCode:returnCode];
}
if (self.didDisplayAlertBlock)
self.didDisplayAlertBlock();
}
#endif
#pragma mark PRIVATE Alert View Delegate Methods
#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
// cancelButtonIndex is set to -1 to show the cancel button up top, but a tap on it ends up here with index 0
if (alertView.cancelButtonIndex == buttonIndex || 0 == buttonIndex) {
// they don't want to rate it
[self dontRate];
} else if (self.showsRemindButton && alertView.firstOtherButtonIndex == buttonIndex) {
// remind them later
[self remindMeLater];
} else {
// they want to rate it
[self _rateApp];
}
}
//Delegate call from the StoreKit view.
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
[self closeModalPanel];
}
//Close the in-app rating (StoreKit) view and restore the previous status bar style.
- (void)closeModalPanel {
if (self.modalPanelOpen) {
[[UIApplication sharedApplication] setStatusBarStyle:self.currentStatusBarStyle animated:self.usesAnimation];
BOOL usedAnimation = self.usesAnimation;
[self setModalPanelOpen:NO];
// get the top most controller (= the StoreKit Controller) and dismiss it
UIViewController *presentingController = [UIApplication sharedApplication].keyWindow.rootViewController;
presentingController = [self topMostViewController:presentingController];
[presentingController dismissViewControllerAnimated:self.usesAnimation completion:^{
if (self.didDismissModalViewBlock)
self.didDismissModalViewBlock(usedAnimation);
}];
[self setCurrentStatusBarStyle:(UIStatusBarStyle)nil];
}
}
#else
- (void)handleNSAlertReturnCode:(NSInteger)returnCode {
switch (returnCode) {
case NSAlertFirstButtonReturn: {
// they want to rate it
[self _rateApp];
break;
}
case NSAlertSecondButtonReturn: {
// remind them later
[self remindMeLater];
break;
}
case NSAlertThirdButtonReturn: {
// they don't want to rate it
[self dontRate];
break;
}
default:
break;
}
}
- (void)alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
[self handleNSAlertReturnCode:returnCode];
}
#endif
- (void)dontRate {
[self.userDefaultsObject setObject:@YES forKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyDeclinedToRate]];
[self.userDefaultsObject synchronize];
if (self.didDeclineToRateBlock)
self.didDeclineToRateBlock();
}
- (void)remindMeLater {
[self.userDefaultsObject setObject:@([[NSDate date] timeIntervalSince1970]) forKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyReminderRequestDate]];
[self.userDefaultsObject synchronize];
if (self.didOptToRemindLaterBlock)
self.didOptToRemindLaterBlock();
}
- (void)_rateApp {
[UAAppReviewManager rateApp];
if (self.didOptToRateBlock)
self.didOptToRateBlock();
}
- (void)rateApp {
[self.userDefaultsObject setObject:@YES forKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyRatedCurrentVersion]];
[self.userDefaultsObject setObject:@YES forKey:[self keyForUAAppReviewManagerKeyType:UAAppReviewManagerKeyRatedAnyVersion]];
[self.userDefaultsObject synchronize];
#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
//Use the in-app StoreKit view if set, available (iOS 6) and imported This works in the simulator.
if (self.opensInStoreKit && NSStringFromClass([SKStoreProductViewController class]) != nil) {
SKStoreProductViewController *storeViewController = [[SKStoreProductViewController alloc] init];
NSNumber *appIDNumber = [NSNumber numberWithInteger:self.appID.integerValue];
[storeViewController loadProductWithParameters:@{ SKStoreProductParameterITunesItemIdentifier : appIDNumber } completionBlock:nil];
storeViewController.delegate = self;
if (self.willPresentModalViewBlock)
self.willPresentModalViewBlock(self.usesAnimation);
[[self getRootViewController] presentViewController:storeViewController animated:self.usesAnimation completion:^{
[self setModalPanelOpen:YES];
//Temporarily use a status bar to match the StoreKit view.
[self setCurrentStatusBarStyle:[UIApplication sharedApplication].statusBarStyle];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
if (UAAppReviewManagerSystemVersionLessThan(@"7.0")) {
// UIStatusBarStyleBlackOpaque is 2
[[UIApplication sharedApplication]setStatusBarStyle:2 animated:self.usesAnimation];
} else {