forked from signalapp/Signal-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiRate.m
executable file
·1107 lines (930 loc) · 44.6 KB
/
iRate.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
//
// iRate.m
//
// Version 1.11.4
//
// Created by Nick Lockwood on 26/01/2011.
// Copyright 2011 Charcoal Design
//
// Distributed under the permissive zlib license
// Get the latest version from here:
//
// https://github.com/nicklockwood/iRate
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
#import "iRate.h"
#import <Availability.h>
#if !__has_feature(objc_arc)
#error This class requires automatic reference counting
#endif
// Unknown diagnostic
// #pragma clang diagnostic ignored "-Wreceiver-is-weak"
#pragma clang diagnostic ignored "-Warc-repeated-use-of-weak"
#pragma clang diagnostic ignored "-Wobjc-missing-property-synthesis"
#pragma clang diagnostic ignored "-Wdirect-ivar-access"
#pragma clang diagnostic ignored "-Wunused-macros"
#pragma clang diagnostic ignored "-Wconversion"
#pragma clang diagnostic ignored "-Wformat-nonliteral"
#pragma clang diagnostic ignored "-Wselector"
#pragma clang diagnostic ignored "-Wgnu"
NSUInteger const iRateAppStoreGameGenreID = 6014;
NSString *const iRateErrorDomain = @"iRateErrorDomain";
NSString *const iRateMessageTitleKey = @"iRateMessageTitle";
NSString *const iRateAppMessageKey = @"iRateAppMessage";
NSString *const iRateGameMessageKey = @"iRateGameMessage";
NSString *const iRateUpdateMessageKey = @"iRateUpdateMessage";
NSString *const iRateCancelButtonKey = @"iRateCancelButton";
NSString *const iRateRemindButtonKey = @"iRateRemindButton";
NSString *const iRateRateButtonKey = @"iRateRateButton";
NSString *const iRateCouldNotConnectToAppStore = @"iRateCouldNotConnectToAppStore";
NSString *const iRateDidDetectAppUpdate = @"iRateDidDetectAppUpdate";
NSString *const iRateDidPromptForRating = @"iRateDidPromptForRating";
NSString *const iRateUserDidAttemptToRateApp = @"iRateUserDidAttemptToRateApp";
NSString *const iRateUserDidDeclineToRateApp = @"iRateUserDidDeclineToRateApp";
NSString *const iRateUserDidRequestReminderToRateApp = @"iRateUserDidRequestReminderToRateApp";
NSString *const iRateDidOpenAppStore = @"iRateDidOpenAppStore";
static NSString *const iRateAppStoreIDKey = @"iRateAppStoreID";
static NSString *const iRateRatedVersionKey = @"iRateRatedVersionChecked";
static NSString *const iRateDeclinedVersionKey = @"iRateDeclinedVersion";
static NSString *const iRateLastRemindedKey = @"iRateLastReminded";
static NSString *const iRateLastVersionUsedKey = @"iRateLastVersionUsed";
static NSString *const iRateFirstUsedKey = @"iRateFirstUsed";
static NSString *const iRateUseCountKey = @"iRateUseCount";
static NSString *const iRateEventCountKey = @"iRateEventCount";
static NSString *const iRateMacAppStoreBundleID = @"com.apple.appstore";
static NSString *const iRateAppLookupURLFormat = @"https://itunes.apple.com/%@/lookup";
static NSString *const iRateiOSAppStoreURLScheme = @"itms-apps";
static NSString *const iRateiOSAppStoreURLFormat = @"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/"
@"viewContentsUserReviews?type=Purple+Software&id=%@&pageNumber=0&"
@"sortOrdering=2&mt=8";
static NSString *const iRateiOS7AppStoreURLFormat = @"itms-apps://itunes.apple.com/app/id%@";
static NSString *const iRateMacAppStoreURLFormat = @"macappstore://itunes.apple.com/app/id%@";
#define SECONDS_IN_A_DAY 86400.0
#define SECONDS_IN_A_WEEK 604800.0
#define MAC_APP_STORE_REFRESH_DELAY 5.0
#define REQUEST_TIMEOUT 60.0
@implementation NSObject (iRate)
- (void)iRateCouldNotConnectToAppStore:(__unused NSError *)error {
}
- (void)iRateDidDetectAppUpdate {
}
- (BOOL)iRateShouldPromptForRating {
return YES;
}
- (void)iRateDidPromptForRating {
}
- (void)iRateUserDidAttemptToRateApp {
}
- (void)iRateUserDidDeclineToRateApp {
}
- (void)iRateUserDidRequestReminderToRateApp {
}
- (BOOL)iRateShouldOpenAppStore {
return YES;
}
- (void)iRateDidOpenAppStore {
}
@end
@interface iRate ()
@property (nonatomic, strong) id visibleAlert;
@property (nonatomic, assign) BOOL checkingForPrompt;
@property (nonatomic, assign) BOOL checkingForAppStoreID;
@property (nonatomic, assign) BOOL shouldPreventPromptAtNextTest;
@end
@implementation iRate
// SIGNAL HACK
// Disabled in Signal. We don't want to prompt users who haven't registered, even if it has been
// a while since they installed. ~mjk
//+ (void)load {
// [self performSelectorOnMainThread:@selector(sharedInstance) withObject:nil waitUntilDone:NO];
//}
+ (instancetype)sharedInstance {
static iRate *sharedInstance = nil;
if (sharedInstance == nil) {
sharedInstance = [(iRate *)[self alloc] init];
}
return sharedInstance;
}
- (NSString *)localizedStringForKey:(NSString *)key withDefault:(NSString *)defaultString {
static NSBundle *bundle = nil;
if (bundle == nil) {
NSString *bundlePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"iRate" ofType:@"bundle"];
if (self.useAllAvailableLanguages) {
bundle = [NSBundle bundleWithPath:bundlePath];
NSString *language = [[NSLocale preferredLanguages] count] ? [NSLocale preferredLanguages][0] : @"en";
if (![[bundle localizations] containsObject:language]) {
language = [language componentsSeparatedByString:@"-"][0];
}
if ([[bundle localizations] containsObject:language]) {
bundlePath = [bundle pathForResource:language ofType:@"lproj"];
}
}
bundle = [NSBundle bundleWithPath:bundlePath] ?: [NSBundle mainBundle];
}
defaultString = [bundle localizedStringForKey:key value:defaultString table:nil];
return [[NSBundle mainBundle] localizedStringForKey:key value:defaultString table:nil];
}
- (iRate *)init {
if ((self = [super init])) {
#if TARGET_OS_IPHONE
// register for iphone application events
if (&UIApplicationWillEnterForegroundNotification) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillEnterForeground)
name:UIApplicationWillEnterForegroundNotification
object:nil];
}
#endif
// get country
self.appStoreCountry = [(NSLocale *)[NSLocale currentLocale] objectForKey:NSLocaleCountryCode];
if ([self.appStoreCountry isEqualToString:@"150"]) {
self.appStoreCountry = @"eu";
} else if (!self.appStoreCountry ||
[[self.appStoreCountry stringByReplacingOccurrencesOfString:@"[A-Za-z]{2}"
withString:@""
options:NSRegularExpressionSearch
range:NSMakeRange(0, 2)] length]) {
self.appStoreCountry = @"us";
}
// application version (use short version preferentially)
self.applicationVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
if ([self.applicationVersion length] == 0) {
self.applicationVersion =
[[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];
}
// localised application name
self.applicationName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
if ([self.applicationName length] == 0) {
self.applicationName = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleNameKey];
}
// bundle id
self.applicationBundleID = [[NSBundle mainBundle] bundleIdentifier];
// default settings
self.useAllAvailableLanguages = YES;
self.promptForNewVersionIfUserRated = NO;
self.onlyPromptIfLatestVersion = YES;
self.onlyPromptIfMainWindowIsAvailable = YES;
self.promptAtLaunch = YES;
self.usesUntilPrompt = 10;
self.eventsUntilPrompt = 10;
self.daysUntilPrompt = 10.0f;
self.usesPerWeekForPrompt = 0.0f;
self.remindPeriod = 1.0f;
self.verboseLogging = NO;
self.previewMode = NO;
self.shouldPreventPromptAtNextTest = NO;
#if DEBUG
// enable verbose logging in debug mode
self.verboseLogging = YES;
NSLog(@"iRate verbose logging enabled.");
#endif
// app launched
[self performSelectorOnMainThread:@selector(applicationLaunched) withObject:nil waitUntilDone:NO];
}
return self;
}
- (id<iRateDelegate>)delegate {
if (_delegate == nil) {
#if TARGET_OS_IPHONE
#define APP_CLASS UIApplication
#else
#define APP_CLASS NSApplication
#endif
_delegate = (id<iRateDelegate>)[[APP_CLASS sharedApplication] delegate];
}
return _delegate;
}
- (NSString *)messageTitle {
return [_messageTitle ?: [self localizedStringForKey:iRateMessageTitleKey withDefault:@"Rate %@"]
stringByReplacingOccurrencesOfString:@"%@"
withString:self.applicationName];
}
- (NSString *)message {
NSString *message = _message;
if (!message) {
message = (self.appStoreGenreID == iRateAppStoreGameGenreID)
? [self localizedStringForKey:iRateGameMessageKey
withDefault:@"If you enjoy playing %@, would you mind taking a moment to rate "
@"it? It won’t take more than a minute. Thanks for your support!"]
: [self localizedStringForKey:iRateAppMessageKey
withDefault:@"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!"];
}
return [message stringByReplacingOccurrencesOfString:@"%@" withString:self.applicationName];
}
- (NSString *)updateMessage {
NSString *updateMessage = _updateMessage;
if (!updateMessage) {
updateMessage = [self localizedStringForKey:iRateUpdateMessageKey withDefault:self.message];
}
return [updateMessage stringByReplacingOccurrencesOfString:@"%@" withString:self.applicationName];
}
- (NSString *)cancelButtonLabel {
return _cancelButtonLabel ?: [self localizedStringForKey:iRateCancelButtonKey withDefault:@"No, Thanks"];
}
- (NSString *)rateButtonLabel {
return _rateButtonLabel ?: [self localizedStringForKey:iRateRateButtonKey withDefault:@"Rate It Now"];
}
- (NSString *)remindButtonLabel {
return _remindButtonLabel ?: [self localizedStringForKey:iRateRemindButtonKey withDefault:@"Remind Me Later"];
}
- (NSURL *)ratingsURL {
if (_ratingsURL) {
return _ratingsURL;
}
if (!self.appStoreID && self.verboseLogging) {
NSLog(@"iRate could not find the App Store ID for this application. If the application is not intended for App "
@"Store release then you must specify a custom ratingsURL.");
}
NSString *URLString;
#if TARGET_OS_IPHONE
float iOSVersion = [[UIDevice currentDevice].systemVersion floatValue];
if (iOSVersion >= 7.0f && iOSVersion < 7.1f) {
URLString = iRateiOS7AppStoreURLFormat;
} else {
URLString = iRateiOSAppStoreURLFormat;
}
#else
URLString = iRateMacAppStoreURLFormat;
#endif
return [NSURL URLWithString:[NSString stringWithFormat:URLString, @(self.appStoreID)]];
}
- (NSUInteger)appStoreID {
return _appStoreID
?: [[[NSUserDefaults standardUserDefaults] objectForKey:iRateAppStoreIDKey] unsignedIntegerValue];
}
- (NSDate *)firstUsed {
return [[NSUserDefaults standardUserDefaults] objectForKey:iRateFirstUsedKey];
}
- (void)setFirstUsed:(NSDate *)date {
[[NSUserDefaults standardUserDefaults] setObject:date forKey:iRateFirstUsedKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (NSDate *)lastReminded {
return [[NSUserDefaults standardUserDefaults] objectForKey:iRateLastRemindedKey];
}
- (void)setLastReminded:(NSDate *)date {
[[NSUserDefaults standardUserDefaults] setObject:date forKey:iRateLastRemindedKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (NSUInteger)usesCount {
return [[NSUserDefaults standardUserDefaults] integerForKey:iRateUseCountKey];
}
- (void)setUsesCount:(NSUInteger)count {
[[NSUserDefaults standardUserDefaults] setInteger:(NSInteger)count forKey:iRateUseCountKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (NSUInteger)eventCount {
return [[NSUserDefaults standardUserDefaults] integerForKey:iRateEventCountKey];
}
- (void)setEventCount:(NSUInteger)count {
[[NSUserDefaults standardUserDefaults] setInteger:(NSInteger)count forKey:iRateEventCountKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (float)usesPerWeek {
return (float)self.usesCount / ([[NSDate date] timeIntervalSinceDate:self.firstUsed] / SECONDS_IN_A_WEEK);
}
- (BOOL)declinedThisVersion {
return [[[NSUserDefaults standardUserDefaults] objectForKey:iRateDeclinedVersionKey]
isEqualToString:self.applicationVersion];
}
- (void)setDeclinedThisVersion:(BOOL)declined {
[[NSUserDefaults standardUserDefaults] setObject:(declined ? self.applicationVersion : nil)
forKey:iRateDeclinedVersionKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (BOOL)declinedAnyVersion {
return [(NSString *)[[NSUserDefaults standardUserDefaults] objectForKey:iRateDeclinedVersionKey] length] != 0;
}
- (BOOL)ratedVersion:(NSString *)version {
return [[[NSUserDefaults standardUserDefaults] objectForKey:iRateRatedVersionKey] isEqualToString:version];
}
- (BOOL)ratedThisVersion {
return [self ratedVersion:self.applicationVersion];
}
- (void)setRatedThisVersion:(BOOL)rated {
[[NSUserDefaults standardUserDefaults] setObject:(rated ? self.applicationVersion : nil)
forKey:iRateRatedVersionKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (BOOL)ratedAnyVersion {
return [(NSString *)[[NSUserDefaults standardUserDefaults] objectForKey:iRateRatedVersionKey] length] != 0;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)preventPromptAtNextTest {
self.shouldPreventPromptAtNextTest = YES;
}
- (void)incrementUseCount {
self.usesCount++;
}
- (void)incrementEventCount {
self.eventCount++;
}
- (BOOL)shouldPromptForRating {
// Avoid potentially inconvenient prompt
if (self.shouldPreventPromptAtNextTest)
{
NSLog(@"iRate did not prompt for rating because it is potentially inconvenient");
self.shouldPreventPromptAtNextTest = NO;
return NO;
}
// preview mode?
if (self.previewMode) {
NSLog(@"iRate preview mode is enabled - make sure you disable this for release");
return YES;
}
// check if we've rated this version
else if (self.ratedThisVersion) {
if (self.verboseLogging) {
NSLog(@"iRate did not prompt for rating because the user has already rated this version");
}
return NO;
}
// check if we've rated any version
else if (self.ratedAnyVersion && !self.promptForNewVersionIfUserRated) {
if (self.verboseLogging) {
NSLog(@"iRate did not prompt for rating because the user has already rated this app, and "
@"promptForNewVersionIfUserRated is disabled");
}
return NO;
}
// check if we've declined to rate the app
else if (self.declinedAnyVersion) {
if (self.verboseLogging) {
NSLog(@"iRate did not prompt for rating because the user has declined to rate the app");
}
return NO;
}
// check how long we've been using this version
else if ([[NSDate date] timeIntervalSinceDate:self.firstUsed] < self.daysUntilPrompt * SECONDS_IN_A_DAY) {
if (self.verboseLogging) {
NSLog(@"iRate did not prompt for rating because the app was first used less than %g days ago",
self.daysUntilPrompt);
}
return NO;
}
// check how many times we've used it and the number of significant events
else if (self.usesCount < self.usesUntilPrompt && self.eventCount < self.eventsUntilPrompt) {
if (self.verboseLogging) {
NSLog(@"iRate did not prompt for rating because the app has only been used %@ times and only %@ events "
@"have been logged",
@(self.usesCount),
@(self.eventCount));
}
return NO;
}
// check if usage frequency is high enough
else if (self.usesPerWeek < self.usesPerWeekForPrompt) {
if (self.verboseLogging) {
NSLog(@"iRate did not prompt for rating because the app has only been used %g times per week on average "
@"since it was installed",
self.usesPerWeek);
}
return NO;
}
// check if within the reminder period
else if (self.lastReminded != nil &&
[[NSDate date] timeIntervalSinceDate:self.lastReminded] < self.remindPeriod * SECONDS_IN_A_DAY) {
if (self.verboseLogging) {
NSLog(@"iRate did not prompt for rating because the user last asked to be reminded less than %g days ago",
self.remindPeriod);
}
return NO;
}
// lets prompt!
return YES;
}
- (NSString *)valueForKey:(NSString *)key inJSON:(id)json {
if ([json isKindOfClass:[NSString class]]) {
// use legacy parser
NSRange keyRange = [json rangeOfString:[NSString stringWithFormat:@"\"%@\"", key]];
if (keyRange.location != NSNotFound) {
NSInteger start = keyRange.location + keyRange.length;
NSRange valueStart = [json rangeOfString:@":"
options:(NSStringCompareOptions)0
range:NSMakeRange(start, [(NSString *)json length] - start)];
if (valueStart.location != NSNotFound) {
start = valueStart.location + 1;
NSRange valueEnd = [json rangeOfString:@","
options:(NSStringCompareOptions)0
range:NSMakeRange(start, [(NSString *)json length] - start)];
if (valueEnd.location != NSNotFound) {
NSString *value = [json substringWithRange:NSMakeRange(start, valueEnd.location - start)];
value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
while ([value hasPrefix:@"\""] && ![value hasSuffix:@"\""]) {
if (valueEnd.location == NSNotFound) {
break;
}
NSInteger newStart = valueEnd.location + 1;
valueEnd = [json rangeOfString:@","
options:(NSStringCompareOptions)0
range:NSMakeRange(newStart, [(NSString *)json length] - newStart)];
value = [json substringWithRange:NSMakeRange(start, valueEnd.location - start)];
value =
[value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
value = [value
stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\""]];
value = [value stringByReplacingOccurrencesOfString:@"\\\\" withString:@"\\"];
value = [value stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];
value = [value stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""];
value = [value stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
value = [value stringByReplacingOccurrencesOfString:@"\\r" withString:@"\r"];
value = [value stringByReplacingOccurrencesOfString:@"\\t" withString:@"\t"];
value = [value stringByReplacingOccurrencesOfString:@"\\f" withString:@"\f"];
value = [value stringByReplacingOccurrencesOfString:@"\\b" withString:@"\f"];
while (YES) {
NSRange unicode = [value rangeOfString:@"\\u"];
if (unicode.location == NSNotFound || unicode.location + unicode.length == 0) {
break;
}
uint32_t c = 0;
NSString *hex = [value substringWithRange:NSMakeRange(unicode.location + 2, 4)];
NSScanner *scanner = [NSScanner scannerWithString:hex];
[scanner scanHexInt:&c];
if (c <= 0xffff) {
value = [value
stringByReplacingCharactersInRange:NSMakeRange(unicode.location, 6)
withString:[NSString stringWithFormat:@"%C", (unichar)c]];
} else {
// convert character to surrogate pair
uint16_t x = (uint16_t)c;
uint16_t u = (c >> 16) & ((1 << 5) - 1);
uint16_t w = (uint16_t)u - 1;
unichar high = 0xd800 | (w << 6) | x >> 10;
unichar low = (uint16_t)(0xdc00 | (x & ((1 << 10) - 1)));
value = [value
stringByReplacingCharactersInRange:NSMakeRange(unicode.location, 6)
withString:[NSString stringWithFormat:@"%C%C", high, low]];
}
}
return value;
}
}
}
} else {
return json[key];
}
return nil;
}
- (void)setAppStoreIDOnMainThread:(NSString *)appStoreIDString {
_appStoreID = [appStoreIDString integerValue];
[[NSUserDefaults standardUserDefaults] setInteger:_appStoreID forKey:iRateAppStoreIDKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)connectionSucceeded {
if (self.checkingForAppStoreID) {
// no longer checking
self.checkingForPrompt = NO;
self.checkingForAppStoreID = NO;
// open app store
[self openRatingsPageInAppStore];
} else if (self.checkingForPrompt) {
// no longer checking
self.checkingForPrompt = NO;
// confirm with delegate
if (![self.delegate iRateShouldPromptForRating]) {
if (self.verboseLogging) {
NSLog(@"iRate did not display the rating prompt because the iRateShouldPromptForRating delegate method "
@"returned NO");
}
return;
}
// prompt user
[self promptForRating];
}
}
- (void)connectionError:(NSError *)error {
if (self.checkingForPrompt || self.checkingForAppStoreID) {
// no longer checking
self.checkingForPrompt = NO;
self.checkingForAppStoreID = NO;
// log the error
if (error) {
NSLog(@"iRate rating process failed because: %@", [error localizedDescription]);
} else {
NSLog(@"iRate rating process failed because an unknown error occured");
}
// could not connect
[self.delegate iRateCouldNotConnectToAppStore:error];
[[NSNotificationCenter defaultCenter] postNotificationName:iRateCouldNotConnectToAppStore object:error];
}
}
- (void)checkForConnectivityInBackground {
if ([NSThread isMainThread]) {
[self performSelectorInBackground:@selector(checkForConnectivityInBackground) withObject:nil];
return;
}
@autoreleasepool {
// prevent concurrent checks
static BOOL checking = NO;
if (checking)
return;
checking = YES;
// first check iTunes
NSString *iTunesServiceURL = [NSString stringWithFormat:iRateAppLookupURLFormat, self.appStoreCountry];
if (_appStoreID) // important that we check ivar and not getter in case it has changed
{
iTunesServiceURL = [iTunesServiceURL stringByAppendingFormat:@"?id=%@", @(_appStoreID)];
} else {
iTunesServiceURL = [iTunesServiceURL stringByAppendingFormat:@"?bundleId=%@", self.applicationBundleID];
}
if (self.verboseLogging) {
NSLog(@"iRate is checking %@ to retrieve the App Store details...", iTunesServiceURL);
}
NSError *error = nil;
NSURLResponse *response = nil;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:iTunesServiceURL]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:REQUEST_TIMEOUT];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSInteger statusCode = ((NSHTTPURLResponse *)response).statusCode;
if (data && statusCode == 200) {
// in case error is garbage...
error = nil;
id json = nil;
if ([NSJSONSerialization class]) {
json = [[NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingOptions)0
error:&error][@"results"] lastObject];
} else {
// convert to string
json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
if (!error) {
NSLog(@"JSON: %@", json);
// check bundle ID matches
NSString *bundleID = [self valueForKey:@"bundleId" inJSON:json];
if (bundleID) {
if ([bundleID isEqualToString:self.applicationBundleID]) {
// get genre
if (self.appStoreGenreID == 0) {
self.appStoreGenreID = [[self valueForKey:@"primaryGenreId" inJSON:json] integerValue];
}
// get app id
if (!_appStoreID) {
NSString *appStoreIDString = [self valueForKey:@"trackId" inJSON:json];
[self performSelectorOnMainThread:@selector(setAppStoreIDOnMainThread:)
withObject:appStoreIDString
waitUntilDone:YES];
if (self.verboseLogging) {
NSLog(@"iRate found the app on iTunes. The App Store ID is %@", appStoreIDString);
}
}
// check version
if (self.onlyPromptIfLatestVersion && !self.previewMode) {
NSString *latestVersion = [self valueForKey:@"version" inJSON:json];
if ([latestVersion compare:self.applicationVersion options:NSNumericSearch] ==
NSOrderedDescending) {
if (self.verboseLogging) {
NSLog(@"iRate found that the installed application version (%@) is not the latest "
@"version on the App Store, which is %@",
self.applicationVersion,
latestVersion);
}
error = [NSError errorWithDomain:iRateErrorDomain
code:iRateErrorApplicationIsNotLatestVersion
userInfo:@{
NSLocalizedDescriptionKey :
@"Installed app is not the latest version available"
}];
}
}
} else {
if (self.verboseLogging) {
NSLog(@"iRate found that the application bundle ID (%@) does not match the bundle ID of "
@"the app found on iTunes (%@) with the specified App Store ID (%@)",
self.applicationBundleID,
bundleID,
@(self.appStoreID));
}
error = [NSError
errorWithDomain:iRateErrorDomain
code:iRateErrorBundleIdDoesNotMatchAppStore
userInfo:@{
NSLocalizedDescriptionKey : [NSString
stringWithFormat:
@"Application bundle ID does not match expected value of %@", bundleID]
}];
}
} else if (_appStoreID || !self.ratingsURL) {
if (self.verboseLogging) {
NSLog(@"iRate could not find this application on iTunes. If your app is not intended for App "
@"Store release then you must specify a custom ratingsURL. If this is the first release "
@"of your application then it's not a problem that it cannot be found on the store yet");
}
if (!self.previewMode) {
error = [NSError errorWithDomain:iRateErrorDomain
code:iRateErrorApplicationNotFoundOnAppStore
userInfo:@{
NSLocalizedDescriptionKey :
@"The application could not be found on the App Store."
}];
}
} else if (!_appStoreID && self.verboseLogging) {
NSLog(@"iRate could not find your app on iTunes. If your app is not yet on the store or is not "
@"intended for App Store release then don't worry about this");
}
}
} else if (statusCode >= 400) {
// http error
NSString *message = [NSString stringWithFormat:@"The server returned a %@ error", @(statusCode)];
error = [NSError errorWithDomain:@"HTTPResponseErrorDomain"
code:statusCode
userInfo:@{NSLocalizedDescriptionKey : message}];
}
// handle errors (ignoring sandbox issues)
if (error && !(error.code == EPERM && [error.domain isEqualToString:NSPOSIXErrorDomain] && _appStoreID)) {
[self performSelectorOnMainThread:@selector(connectionError:) withObject:error waitUntilDone:YES];
} else if (self.appStoreID || self.previewMode) {
// show prompt
[self performSelectorOnMainThread:@selector(connectionSucceeded) withObject:nil waitUntilDone:YES];
}
// finished
checking = NO;
}
}
- (void)promptIfNetworkAvailable {
if (!self.checkingForPrompt && !self.checkingForAppStoreID) {
self.checkingForPrompt = YES;
[self checkForConnectivityInBackground];
}
}
- (void)promptIfAllCriteriaMet {
if ([self shouldPromptForRating]) {
[self promptIfNetworkAvailable];
}
}
- (BOOL)showRemindButton {
return [self.remindButtonLabel length];
}
- (BOOL)showCancelButton {
return [self.cancelButtonLabel length];
}
- (void)promptForRating {
if (!self.visibleAlert) {
NSString *message = self.ratedAnyVersion ? self.updateMessage : self.message;
#if TARGET_OS_IPHONE
UIViewController *topController = [UIApplication sharedApplication].delegate.window.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
if ([UIAlertController class] && topController && self.useUIAlertControllerIfAvailable) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:self.messageTitle
message:message
preferredStyle:UIAlertControllerStyleAlert];
// rate action
[alert addAction:[UIAlertAction actionWithTitle:self.rateButtonLabel
style:UIAlertActionStyleDefault
handler:^(__unused UIAlertAction *action) {
[self didDismissAlert:alert withButtonAtIndex:0];
}]];
// cancel action
if ([self showCancelButton]) {
[alert addAction:[UIAlertAction actionWithTitle:self.cancelButtonLabel
style:UIAlertActionStyleCancel
handler:^(__unused UIAlertAction *action) {
[self didDismissAlert:alert withButtonAtIndex:1];
}]];
}
// remind action
if ([self showRemindButton]) {
[alert addAction:[UIAlertAction actionWithTitle:self.remindButtonLabel
style:UIAlertActionStyleDefault
handler:^(__unused UIAlertAction *action) {
[self didDismissAlert:alert
withButtonAtIndex:[self showCancelButton] ? 2 : 1];
}]];
}
self.visibleAlert = alert;
// get current view controller and present alert
[topController presentViewController:alert animated:YES completion:NULL];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:self.messageTitle
message:message
delegate:(id<UIAlertViewDelegate>)self
cancelButtonTitle:nil
otherButtonTitles:self.rateButtonLabel, nil];
if ([self showCancelButton]) {
[alert addButtonWithTitle:self.cancelButtonLabel];
alert.cancelButtonIndex = 1;
}
if ([self showRemindButton]) {
[alert addButtonWithTitle:self.remindButtonLabel];
}
self.visibleAlert = alert;
[self.visibleAlert show];
}
#else
// only show when main window is available
if (self.onlyPromptIfMainWindowIsAvailable && ![[NSApplication sharedApplication] mainWindow]) {
[self performSelector:@selector(promptForRating) withObject:nil afterDelay:0.5];
return;
}
NSAlert *alert = [[NSAlert alloc] init];
alert.messageText = self.messageTitle;
alert.informativeText = message;
[alert addButtonWithTitle:self.rateButtonLabel];
if ([self showCancelButton]) {
[alert addButtonWithTitle:self.cancelButtonLabel];
}
if ([self showRemindButton]) {
[alert addButtonWithTitle:self.remindButtonLabel];
}
self.visibleAlert = alert;
#if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_9
if (![alert respondsToSelector:@selector(beginSheetModalForWindow:completionHandler:)]) {
[alert beginSheetModalForWindow:[NSApplication sharedApplication].mainWindow
modalDelegate:self
didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
contextInfo:nil];
} else
#endif
{
[alert beginSheetModalForWindow:[NSApplication sharedApplication].mainWindow
completionHandler:^(NSModalResponse returnCode) {
[self didDismissAlert:alert withButtonAtIndex:returnCode - NSAlertFirstButtonReturn];
}];
}
#endif
// inform about prompt
[self.delegate iRateDidPromptForRating];
[[NSNotificationCenter defaultCenter] postNotificationName:iRateDidPromptForRating object:nil];
}
}
- (void)applicationLaunched {
// check if this is a new version
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *lastUsedVersion = [defaults objectForKey:iRateLastVersionUsedKey];
if (!self.firstUsed || ![lastUsedVersion isEqualToString:self.applicationVersion]) {
[defaults setObject:self.applicationVersion forKey:iRateLastVersionUsedKey];
if (!self.firstUsed || [self ratedAnyVersion]) {
// reset defaults
[defaults setObject:[NSDate date] forKey:iRateFirstUsedKey];
[defaults setInteger:0 forKey:iRateUseCountKey];
[defaults setInteger:0 forKey:iRateEventCountKey];
[defaults setObject:nil forKey:iRateLastRemindedKey];
[defaults synchronize];
} else if ([[NSDate date] timeIntervalSinceDate:self.firstUsed] >
(self.daysUntilPrompt - 1) * SECONDS_IN_A_DAY) {
// if was previously installed, but we haven't yet prompted for a rating
// don't reset, but make sure it won't rate for a day at least
self.firstUsed = [[NSDate date] dateByAddingTimeInterval:(self.daysUntilPrompt - 1) * -SECONDS_IN_A_DAY];
}
// inform about app update
[self.delegate iRateDidDetectAppUpdate];
[[NSNotificationCenter defaultCenter] postNotificationName:iRateDidDetectAppUpdate object:nil];
}
[self incrementUseCount];
if (self.shouldPromptForRating) {
[self promptForRating];
}
}
- (void)didDismissAlert:(__unused id)alertView withButtonAtIndex:(NSInteger)buttonIndex {
// get button indices
NSInteger rateButtonIndex = 0;
NSInteger cancelButtonIndex = [self showCancelButton] ? 1 : 0;
NSInteger remindButtonIndex = [self showRemindButton] ? cancelButtonIndex + 1 : 0;
if (buttonIndex == rateButtonIndex) {
[self rate];
} else if (buttonIndex == cancelButtonIndex) {
[self declineThisVersion];
} else if (buttonIndex == remindButtonIndex) {
[self remindLater];
}
// release alert
self.visibleAlert = nil;
}
#if TARGET_OS_IPHONE
- (void)applicationWillEnterForeground {
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
[self incrementUseCount];
if (self.promptAtLaunch) {
[self promptIfAllCriteriaMet];
}
}
}
- (void)openRatingsPageInAppStore {
if (!_ratingsURL && !self.appStoreID) {
self.checkingForAppStoreID = YES;
if (!self.checkingForPrompt) {
[self checkForConnectivityInBackground];
}
return;
}
NSString *cantOpenMessage = nil;
#if TARGET_IPHONE_SIMULATOR
if ([[self.ratingsURL scheme] isEqualToString:iRateiOSAppStoreURLScheme]) {
cantOpenMessage =
@"iRate could not open the ratings page because the App Store is not available on the iOS simulator";
}
#elif DEBUG
if (![[UIApplication sharedApplication] canOpenURL:self.ratingsURL]) {
cantOpenMessage =
[NSString stringWithFormat:@"iRate was unable to open the specified ratings URL: %@", self.ratingsURL];
}
#endif