forked from arashpayan/appirater
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Ask4AppReviews.m
executable file
·733 lines (575 loc) · 26.5 KB
/
Ask4AppReviews.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
/*
This file is part of Ask4AppReviews (forked from Appirater).
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* Ask4AppReviews.m
* Ask4AppReviews
*
* Created by Luke Durrant on 7/12.
* Ask4AppReviews (forked from Appirater).
*/
#import "Ask4AppReviews.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
NSString *const kAsk4AppReviewsFirstUseDate = @"kAsk4AppReviewsFirstUseDate";
NSString *const kAsk4AppReviewsUseCount = @"kAsk4AppReviewsUseCount";
NSString *const kAsk4AppReviewsSignificantEventCount = @"kAsk4AppReviewsSignificantEventCount";
NSString *const kAsk4AppReviewsCurrentVersion = @"kAsk4AppReviewsCurrentVersion";
NSString *const kAsk4AppReviewsRatedCurrentVersion = @"kAsk4AppReviewsRatedCurrentVersion";
NSString *const kAsk4AppReviewsDeclinedToRate = @"kAsk4AppReviewsDeclinedToRate";
NSString *const kAsk4AppReviewsReminderRequestDate = @"kAsk4AppReviewsReminderRequestDate";
NSString *const kAsk4AppReviewsAppIdBundleKey = @"AppStoreId";
NSString *const kAsk4AppReviewsEmailBundleKey = @"DeveloperEmail";
static double _daysUntilPrompt = 30;
static NSInteger _usesUntilPrompt = 20;
static NSInteger _significantEventsUntilPrompt = -1;
static double _timeBeforeReminding = 1;
static BOOL _debug = NO;
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_5_0
static id<Ask4AppReviewsDelegate> _delegate;
#else
__weak static id<Ask4AppReviewsDelegate> _delegate;
#endif
static BOOL _usesAnimation = TRUE;
static UIStatusBarStyle _statusBarStyle;
static BOOL _modalOpen = false;
NSString *templateReviewURL = @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=APP_ID";
NSString *templateReviewURLiOS7 = @"itms-apps://itunes.apple.com/app/idAPP_ID";
@interface Ask4AppReviews ()
- (BOOL)connectedToNetwork;
+ (NSString*)appStoreAppID;
+ (NSString*)developerEmail;
+ (Ask4AppReviews*)sharedInstance;
- (void)showRatingAlert;
- (void)showQuestionAlert;
- (BOOL)ratingConditionsHaveBeenMet;
- (void)incrementUseCount;
- (void)hideRatingAlert;
@end
@implementation Ask4AppReviews {
}
@synthesize questionAlert;
@synthesize ratingAlert;
@synthesize theViewController;
+ (void) setDaysUntilPrompt:(double)value {
_daysUntilPrompt = value;
}
+ (void) setUsesUntilPrompt:(NSInteger)value {
_usesUntilPrompt = value;
}
+ (void) setSignificantEventsUntilPrompt:(NSInteger)value {
_significantEventsUntilPrompt = value;
}
+ (void) setTimeBeforeReminding:(double)value {
_timeBeforeReminding = value;
}
+ (void) setDebug:(BOOL)debug {
_debug = debug;
}
+ (void)setDelegate:(id<Ask4AppReviewsDelegate>)delegate{
_delegate = delegate;
}
+ (void)setUsesAnimation:(BOOL)animation {
_usesAnimation = animation;
}
+ (void)setOpenInAppStore:(BOOL)openInAppStore {
[Ask4AppReviews sharedInstance].openInAppStore = openInAppStore;
}
+ (void)setStatusBarStyle:(UIStatusBarStyle)style {
_statusBarStyle = style;
}
+ (void)setModalOpen:(BOOL)open {
_modalOpen = open;
}
- (BOOL)connectedToNetwork {
// Create zero addy
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
// Recover reachability flags
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);
if (!didRetrieveFlags)
{
// NSLog(@"Error. Could not recover network reachability flags");
return NO;
}
BOOL isReachable = flags & kSCNetworkFlagsReachable;
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;
NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"];
NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0];
NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:self];
return ((isReachable && !needsConnection) || nonWiFi) ? (testConnection ? YES : NO) : NO;
}
+(NSString*)appStoreAppID
{
NSString* value = [[NSBundle mainBundle] infoDictionary][kAsk4AppReviewsAppIdBundleKey];
NSAssert1(value, @"Error - you have not specified %@ property in your info.plist", kAsk4AppReviewsAppIdBundleKey);
return value;
}
+(NSString*)developerEmail {
NSString* value = [[NSBundle mainBundle] infoDictionary][kAsk4AppReviewsEmailBundleKey];
NSAssert1(value, @"Error - you have not specified %@ property in your info.plist", kAsk4AppReviewsEmailBundleKey);
return value;
}
+ (Ask4AppReviews*)sharedInstance {
static Ask4AppReviews *ask = nil;
if (ask == nil)
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
ask = [[Ask4AppReviews alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive) name:
UIApplicationWillResignActiveNotification object:nil];
});
}
return ask;
}
- (id)init
{
self = [super init];
if (self) {
if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) {
self.openInAppStore = YES;
} else {
self.openInAppStore = NO;
}
}
return self;
}
- (void)showRatingAlert {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:Ask4AppReviews_MESSAGE_TITLE
message:Ask4AppReviews_MESSAGE
delegate:self
cancelButtonTitle:Ask4AppReviews_CANCEL_BUTTON
otherButtonTitles:Ask4AppReviews_RATE_BUTTON, Ask4AppReviews_RATE_LATER, nil];
alertView.tag = 2;
self.ratingAlert = alertView;
[alertView show];
}
- (void)showQuestionAlert
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:Ask4AppReviews_QUESTION_MESSAGE_TITLE
message:Ask4AppReviews_QUESTION
delegate:self
cancelButtonTitle:Ask4AppReviews_RATE_LATER
otherButtonTitles:Ask4AppReviews_NO, Ask4AppReviews_YES, nil];
alertView.tag = 1;
self.questionAlert = alertView;
[alertView show];
}
- (BOOL)ratingConditionsHaveBeenMet {
if (_debug)
return YES;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSDate *dateOfFirstLaunch = [NSDate dateWithTimeIntervalSince1970:[userDefaults doubleForKey:kAsk4AppReviewsFirstUseDate]];
NSTimeInterval timeSinceFirstLaunch = [[NSDate date] timeIntervalSinceDate:dateOfFirstLaunch];
NSTimeInterval timeUntilRate = 60 * 60 * 24 * _daysUntilPrompt;
if (timeSinceFirstLaunch < timeUntilRate)
return NO;
// check if the app has been used enough
int useCount = [userDefaults integerForKey:kAsk4AppReviewsUseCount];
if (useCount <= _usesUntilPrompt)
return NO;
// check if the user has done enough significant events
int sigEventCount = [userDefaults integerForKey:kAsk4AppReviewsSignificantEventCount];
if (sigEventCount <= _significantEventsUntilPrompt)
return NO;
// has the user previously declined to rate this version of the app?
if ([userDefaults boolForKey:kAsk4AppReviewsDeclinedToRate])
return NO;
// has the user already rated the app?
if ([userDefaults boolForKey:kAsk4AppReviewsRatedCurrentVersion])
return NO;
// if the user wanted to be reminded later, has enough time passed?
NSDate *reminderRequestDate = [NSDate dateWithTimeIntervalSince1970:[userDefaults doubleForKey:kAsk4AppReviewsReminderRequestDate]];
NSTimeInterval timeSinceReminderRequest = [[NSDate date] timeIntervalSinceDate:reminderRequestDate];
NSTimeInterval timeUntilReminder = 60 * 60 * 24 * _timeBeforeReminding;
if (timeSinceReminderRequest < timeUntilReminder)
return NO;
return YES;
}
- (void)incrementUseCount {
// get the app's version
NSString *version = [[NSBundle mainBundle] infoDictionary][(__bridge NSString*)kCFBundleVersionKey];
// get the version number that we've been tracking
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *trackingVersion = [userDefaults stringForKey:kAsk4AppReviewsCurrentVersion];
if (trackingVersion == nil)
{
trackingVersion = version;
[userDefaults setObject:version forKey:kAsk4AppReviewsCurrentVersion];
}
if (_debug)
NSLog(@"Ask4AppReviews Tracking version: %@", trackingVersion);
if ([trackingVersion isEqualToString:version])
{
// check if the first use date has been set. if not, set it.
NSTimeInterval timeInterval = [userDefaults doubleForKey:kAsk4AppReviewsFirstUseDate];
if (timeInterval == 0)
{
timeInterval = [[NSDate date] timeIntervalSince1970];
[userDefaults setDouble:timeInterval forKey:kAsk4AppReviewsFirstUseDate];
}
// increment the use count
int useCount = [userDefaults integerForKey:kAsk4AppReviewsUseCount];
useCount++;
[userDefaults setInteger:useCount forKey:kAsk4AppReviewsUseCount];
if (_debug)
NSLog(@"Ask4AppReviews Use count: %d", useCount);
}
else
{
// it's a new version of the app, so restart tracking
[userDefaults setObject:version forKey:kAsk4AppReviewsCurrentVersion];
[userDefaults setDouble:[[NSDate date] timeIntervalSince1970] forKey:kAsk4AppReviewsFirstUseDate];
[userDefaults setInteger:1 forKey:kAsk4AppReviewsUseCount];
[userDefaults setInteger:0 forKey:kAsk4AppReviewsSignificantEventCount];
[userDefaults setBool:NO forKey:kAsk4AppReviewsRatedCurrentVersion];
[userDefaults setBool:NO forKey:kAsk4AppReviewsDeclinedToRate];
[userDefaults setDouble:0 forKey:kAsk4AppReviewsReminderRequestDate];
}
[userDefaults synchronize];
}
- (void)incrementSignificantEventCount {
// get the app's version
NSString *version = [[NSBundle mainBundle] infoDictionary][(__bridge NSString*)kCFBundleVersionKey];
// get the version number that we've been tracking
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *trackingVersion = [userDefaults stringForKey:kAsk4AppReviewsCurrentVersion];
if (trackingVersion == nil)
{
trackingVersion = version;
[userDefaults setObject:version forKey:kAsk4AppReviewsCurrentVersion];
}
if (_debug)
NSLog(@"Ask4AppReviews Tracking version: %@", trackingVersion);
if ([trackingVersion isEqualToString:version])
{
// check if the first use date has been set. if not, set it.
NSTimeInterval timeInterval = [userDefaults doubleForKey:kAsk4AppReviewsFirstUseDate];
if (timeInterval == 0)
{
timeInterval = [[NSDate date] timeIntervalSince1970];
[userDefaults setDouble:timeInterval forKey:kAsk4AppReviewsFirstUseDate];
}
// increment the significant event count
int sigEventCount = [userDefaults integerForKey:kAsk4AppReviewsSignificantEventCount];
sigEventCount++;
[userDefaults setInteger:sigEventCount forKey:kAsk4AppReviewsSignificantEventCount];
if (_debug)
NSLog(@"Ask4AppReviews Significant event count: %d", sigEventCount);
}
else
{
// it's a new version of the app, so restart tracking
[userDefaults setObject:version forKey:kAsk4AppReviewsCurrentVersion];
[userDefaults setDouble:0 forKey:kAsk4AppReviewsFirstUseDate];
[userDefaults setInteger:0 forKey:kAsk4AppReviewsUseCount];
[userDefaults setInteger:1 forKey:kAsk4AppReviewsSignificantEventCount];
[userDefaults setBool:NO forKey:kAsk4AppReviewsRatedCurrentVersion];
[userDefaults setBool:NO forKey:kAsk4AppReviewsDeclinedToRate];
[userDefaults setDouble:0 forKey:kAsk4AppReviewsReminderRequestDate];
}
[userDefaults synchronize];
}
- (void)incrementAndRate:(BOOL)canPromptForRating {
[self incrementUseCount];
if (canPromptForRating &&
[self ratingConditionsHaveBeenMet] &&
[self connectedToNetwork])
{
dispatch_async(dispatch_get_main_queue(),
^{
//If they have been asked before and said remind me take them straight to the rating alert
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
double kAsk4AppReviewsReminder = [userDefaults doubleForKey:kAsk4AppReviewsReminderRequestDate];
if(kAsk4AppReviewsReminder == 0 || _debug)
{
[self showQuestionAlert];
}else{
[self showRatingAlert];
}
});
}
}
- (void)incrementSignificantEventAndRate:(BOOL)canPromptForRating {
[self incrementSignificantEventCount];
if (canPromptForRating &&
[self ratingConditionsHaveBeenMet] &&
[self connectedToNetwork])
{
dispatch_async(dispatch_get_main_queue(),
^{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
double kAsk4AppReviewsReminder = [userDefaults doubleForKey:kAsk4AppReviewsReminderRequestDate];
if(kAsk4AppReviewsReminder == 0 || _debug)
{
[self showQuestionAlert];
}else{
[self showRatingAlert];
}
});
}
}
+ (void)appLaunched {
[Ask4AppReviews appLaunched:YES];
}
+ (void)askQuestion{
[[Ask4AppReviews sharedInstance] showQuestionAlert];
}
+ (void)appLaunched:(BOOL)canPromptForRating {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),
^{
[[Ask4AppReviews sharedInstance] incrementAndRate:canPromptForRating];
});
}
+ (void)appLaunched:(BOOL)canPromptForRating viewController:(UINavigationController*)viewController {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),
^{
[[Ask4AppReviews sharedInstance] setTheViewController:viewController];
[[Ask4AppReviews sharedInstance] incrementAndRate:canPromptForRating];
});
}
- (void)hideRatingAlert {
if (self.ratingAlert.visible) {
if (_debug)
NSLog(@"Ask4AppReviews Hiding Alert");
[self.ratingAlert dismissWithClickedButtonIndex:-1 animated:NO];
}
if (self.questionAlert.visible) {
if (_debug)
NSLog(@"Ask4AppReviews questionAlert Alert");
[self.questionAlert dismissWithClickedButtonIndex:-1 animated:NO];
}
}
+ (void)appWillResignActive {
if (_debug)
NSLog(@"Ask4AppReviews appWillResignActive");
[[Ask4AppReviews sharedInstance] hideRatingAlert];
}
+ (void)appEnteredForeground:(BOOL)canPromptForRating {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),
^{
[[Ask4AppReviews sharedInstance] incrementAndRate:canPromptForRating];
});
}
+ (void)userDidSignificantEvent:(BOOL)canPromptForRating {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),
^{
[[Ask4AppReviews sharedInstance] incrementSignificantEventAndRate:canPromptForRating];
});
}
+ (void)rateApp {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:YES forKey:kAsk4AppReviewsCurrentVersion];
[userDefaults synchronize];
//Use the in-app StoreKit view if available (iOS 6) and imported. This works in the simulator.
if (![Ask4AppReviews sharedInstance].openInAppStore && NSStringFromClass([SKStoreProductViewController class]) != nil) {
SKStoreProductViewController *storeViewController = [[SKStoreProductViewController alloc] init];
NSNumber *appId = [NSNumber numberWithInteger:[Ask4AppReviews appStoreAppID].integerValue];
[storeViewController loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier:appId} completionBlock:nil];
storeViewController.delegate = self.sharedInstance;
id <Ask4AppReviewsDelegate> delegate = self.sharedInstance.delegate;
if ([delegate respondsToSelector:@selector(ask4AppReviewsWillPresentModalView:animated:)]) {
[delegate ask4AppReviewsWillPresentModalView:self.sharedInstance animated:_usesAnimation];
}
[[self getRootViewController] presentViewController:storeViewController animated:_usesAnimation completion:^{
[self setModalOpen:YES];
//Temporarily use a black status bar to match the StoreKit view.
[self setStatusBarStyle:[UIApplication sharedApplication].statusBarStyle];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
[[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent animated:_usesAnimation];
#endif
}];
//Use the standard openUrl method if StoreKit is unavailable.
} else {
#if TARGET_IPHONE_SIMULATOR
NSLog(@"ASK4AppReviews NOTE: iTunes App Store is not supported on the iOS simulator. Unable to open App Store page.");
#else
NSString *reviewURL = [templateReviewURL stringByReplacingOccurrencesOfString:@"APP_ID" withString:[NSString stringWithFormat:@"%@", [Ask4AppReviews appStoreAppID]]];
// iOS 7 needs a different templateReviewURL @see https://github.com/arashpayan/appirater/issues/131
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0 && [[[UIDevice currentDevice] systemVersion] floatValue] < 7.1) {
reviewURL = [templateReviewURLiOS7 stringByReplacingOccurrencesOfString:@"APP_ID" withString:[NSString stringWithFormat:@"%@", [Ask4AppReviews appStoreAppID]]];
}
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:reviewURL]];
#endif
}
}
+ (id)getRootViewController {
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
if (window.windowLevel != UIWindowLevelNormal) {
NSArray *windows = [[UIApplication sharedApplication] windows];
for(window in windows) {
if (window.windowLevel == UIWindowLevelNormal) {
break;
}
}
}
for (UIView *subView in [window subviews])
{
UIResponder *responder = [subView nextResponder];
if([responder isKindOfClass:[UIViewController class]]) {
return [self topMostViewController: (UIViewController *) responder];
}
}
return nil;
}
+ (UIViewController *) topMostViewController: (UIViewController *) controller {
BOOL isPresenting = NO;
do {
// this path is called only on iOS 6+, so -presentedViewController is fine here.
UIViewController *presented = [controller presentedViewController];
isPresenting = presented != nil;
if(presented != nil) {
controller = presented;
}
} while (isPresenting);
return controller;
}
//Delegate Methods
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[[Ask4AppReviews getRootViewController] dismissModalViewControllerAnimated:YES];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
//NSLog(@"tag %i buttonIndex %i ", alertView.tag, buttonIndex);
id <Ask4AppReviewsDelegate> delegate = _delegate;
if(alertView.tag == 1)
{
//we are asking to rate, remind, or no thanks
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
switch (buttonIndex) {
case 0:
{
// remind them later
[userDefaults setDouble:[[NSDate date] timeIntervalSince1970] forKey:kAsk4AppReviewsReminderRequestDate];
[userDefaults synchronize];
if(delegate && [delegate respondsToSelector:@selector(ask4AppReviewsDidOptToRemindLater:)]){
[delegate ask4AppReviewsDidOptToRemindLater:self];
}
break;
}
case 1:
{
//No
[self showRatingAlert];
break;
}
case 2:
//They have issues ask them to fill in a email question
//You need to include UIMessage Framework
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mPicker = [[MFMailComposeViewController alloc] init];
mPicker.mailComposeDelegate = self;
[mPicker setSubject: Ask4AppReviews_EMAIL_SUBJECT];
NSArray *toRecipients = @[[Ask4AppReviews developerEmail]];
[mPicker setToRecipients:toRecipients];
// GM - Added some custom code here to provide system info in the support email.
UIDevice *currentDevice = [UIDevice currentDevice];
NSString *model = [currentDevice model];
NSString *systemVersion = [currentDevice systemVersion];
NSArray *languageArray = [NSLocale preferredLanguages];
NSString *language = languageArray[0];
NSLocale *locale = [NSLocale currentLocale];
NSString *country = [locale localeIdentifier];
NSString *appVersion = [[NSBundle mainBundle]
objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleVersionKey];
NSString *deviceSpecs =
[NSString stringWithFormat:@"%@ \n\n\n\n\n\n %@ \n OS - %@ \n Lang - %@ \n Country - %@ \n App Version - %@",
Ask4AppReviews_EMAIL_BODY, model, systemVersion, language, country, appVersion];
NSLog(@"Device Specs --> %@",deviceSpecs);
[mPicker setMessageBody:deviceSpecs isHTML:NO];
[[Ask4AppReviews getRootViewController] presentViewController:mPicker animated:YES completion:^{
}];
}else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
message:Ask4AppReviews_DEVELOPER_EMAIL_ALERT
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
}
//We dont want them to rate it
[userDefaults setBool:YES forKey:kAsk4AppReviewsDeclinedToRate];
[userDefaults synchronize];
break;
default:
break;
}
}else if(alertView.tag == 2)
{
//we are asking if the have any issues using app no, yes or remind me later
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
switch (buttonIndex) {
case 0:
{
// they don't want to rate it
[userDefaults setBool:YES forKey:kAsk4AppReviewsDeclinedToRate];
[userDefaults synchronize];
break;
}
case 1:
{
// they want to rate it
[Ask4AppReviews rateApp];
break;
}
case 2:
// remind them later
[userDefaults setDouble:[[NSDate date] timeIntervalSince1970] forKey:kAsk4AppReviewsReminderRequestDate];
[userDefaults synchronize];
break;
default:
break;
}
}
}
//Delegate call from the StoreKit view.
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
[Ask4AppReviews closeModal];
}
//Close the in-app rating (StoreKit) view and restore the previous status bar style.
+ (void)closeModal {
if (_modalOpen) {
[[UIApplication sharedApplication]setStatusBarStyle:_statusBarStyle animated:_usesAnimation];
BOOL usedAnimation = _usesAnimation;
[self setModalOpen:NO];
// get the top most controller (= the StoreKit Controller) and dismiss it
UIViewController *presentingController = [UIApplication sharedApplication].keyWindow.rootViewController;
presentingController = [self topMostViewController: presentingController];
[presentingController dismissViewControllerAnimated:_usesAnimation completion:^{
id <Ask4AppReviewsDelegate> delegate = self.sharedInstance.delegate;
if ([delegate respondsToSelector:@selector(ask4AppReviewsDidDismissModalView:animated:)]) {
[delegate ask4AppReviewsDidDismissModalView:(Ask4AppReviews *)self animated:usedAnimation];
}
}];
[self.class setStatusBarStyle:(UIStatusBarStyle)nil];
}
}
@end