4504
|
1 |
/*
|
|
2 |
This file is part of Appirater.
|
|
3 |
|
|
4 |
Copyright (c) 2010, Arash Payan
|
|
5 |
All rights reserved.
|
|
6 |
|
|
7 |
Permission is hereby granted, free of charge, to any person
|
|
8 |
obtaining a copy of this software and associated documentation
|
|
9 |
files (the "Software"), to deal in the Software without
|
|
10 |
restriction, including without limitation the rights to use,
|
|
11 |
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
12 |
copies of the Software, and to permit persons to whom the
|
|
13 |
Software is furnished to do so, subject to the following
|
|
14 |
conditions:
|
|
15 |
|
|
16 |
The above copyright notice and this permission notice shall be
|
|
17 |
included in all copies or substantial portions of the Software.
|
|
18 |
|
|
19 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
20 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
21 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
22 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
23 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
24 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
25 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
26 |
OTHER DEALINGS IN THE SOFTWARE.
|
|
27 |
*/
|
|
28 |
/*
|
|
29 |
* Appirater.m
|
|
30 |
* appirater
|
|
31 |
*
|
|
32 |
* Created by Arash Payan on 9/5/09.
|
|
33 |
* http://arashpayan.com
|
|
34 |
* Copyright 2010 Arash Payan. All rights reserved.
|
|
35 |
*/
|
|
36 |
|
|
37 |
#import "Appirater.h"
|
|
38 |
#import <SystemConfiguration/SCNetworkReachability.h>
|
|
39 |
#import <netinet/in.h>
|
|
40 |
|
|
41 |
NSString *const kAppiraterLaunchDate = @"kAppiraterLaunchDate";
|
|
42 |
NSString *const kAppiraterLaunchCount = @"kAppiraterLaunchCount";
|
|
43 |
NSString *const kAppiraterCurrentVersion = @"kAppiraterCurrentVersion";
|
|
44 |
NSString *const kAppiraterRatedCurrentVersion = @"kAppiraterRatedCurrentVersion";
|
|
45 |
NSString *const kAppiraterDeclinedToRate = @"kAppiraterDeclinedToRate";
|
|
46 |
|
|
47 |
NSString *templateReviewURL = @"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=APP_ID&onlyLatestVersion=true&pageNumber=0&sortOrdering=1&type=Purple+Software";
|
|
48 |
|
|
49 |
@implementation Appirater
|
|
50 |
|
|
51 |
+(void) appLaunched {
|
|
52 |
Appirater *appirater = [[Appirater alloc] init];
|
|
53 |
[NSThread detachNewThreadSelector:@selector(appLaunchedHandler) toTarget:appirater withObject:nil];
|
|
54 |
}
|
|
55 |
|
|
56 |
-(void) appLaunchedHandler {
|
|
57 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
58 |
|
|
59 |
if (APPIRATER_DEBUG) {
|
|
60 |
[self performSelectorOnMainThread:@selector(showPrompt) withObject:nil waitUntilDone:NO];
|
|
61 |
return;
|
|
62 |
}
|
|
63 |
|
|
64 |
BOOL willShowPrompt = NO;
|
|
65 |
|
|
66 |
// get the app's version
|
|
67 |
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey];
|
|
68 |
|
|
69 |
// get the version number that we've been tracking
|
|
70 |
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
|
|
71 |
NSString *trackingVersion = [userDefaults stringForKey:kAppiraterCurrentVersion];
|
|
72 |
if (trackingVersion == nil) {
|
|
73 |
trackingVersion = version;
|
|
74 |
[userDefaults setObject:version forKey:kAppiraterCurrentVersion];
|
|
75 |
}
|
|
76 |
|
|
77 |
if (APPIRATER_DEBUG)
|
|
78 |
DLog(@"APPIRATER Tracking version: %@", trackingVersion);
|
|
79 |
|
|
80 |
if ([trackingVersion isEqualToString:version]) {
|
|
81 |
// get the launch date
|
|
82 |
NSTimeInterval timeInterval = [userDefaults doubleForKey:kAppiraterLaunchDate];
|
|
83 |
if (timeInterval == 0) {
|
|
84 |
timeInterval = [[NSDate date] timeIntervalSince1970];
|
|
85 |
[userDefaults setDouble:timeInterval forKey:kAppiraterLaunchDate];
|
|
86 |
}
|
|
87 |
|
|
88 |
NSTimeInterval secondsSinceLaunch = [[NSDate date] timeIntervalSinceDate:[NSDate dateWithTimeIntervalSince1970:timeInterval]];
|
|
89 |
double secondsUntilPrompt = 60 * 60 * 24 * DAYS_UNTIL_PROMPT;
|
|
90 |
|
|
91 |
// get the launch count
|
|
92 |
int launchCount = [userDefaults integerForKey:kAppiraterLaunchCount];
|
|
93 |
launchCount++;
|
|
94 |
[userDefaults setInteger:launchCount forKey:kAppiraterLaunchCount];
|
|
95 |
if (APPIRATER_DEBUG)
|
|
96 |
NSLog(@"APPIRATER Launch count: %d", launchCount);
|
|
97 |
|
|
98 |
// have they previously declined to rate this version of the app?
|
|
99 |
BOOL declinedToRate = [userDefaults boolForKey:kAppiraterDeclinedToRate];
|
|
100 |
|
|
101 |
// have they already rated the app?
|
|
102 |
BOOL ratedApp = [userDefaults boolForKey:kAppiraterRatedCurrentVersion];
|
|
103 |
|
|
104 |
if (secondsSinceLaunch > secondsUntilPrompt &&
|
|
105 |
launchCount > LAUNCHES_UNTIL_PROMPT &&
|
|
106 |
!declinedToRate &&
|
|
107 |
!ratedApp) {
|
6246
|
108 |
if ([HWUtils isNetworkReachable]) { // check if they can reach the app store
|
4504
|
109 |
willShowPrompt = YES;
|
|
110 |
[self performSelectorOnMainThread:@selector(showPrompt) withObject:nil waitUntilDone:NO];
|
|
111 |
}
|
|
112 |
}
|
|
113 |
} else {
|
|
114 |
// it's a new version of the app, so restart tracking
|
|
115 |
[userDefaults setObject:version forKey:kAppiraterCurrentVersion];
|
|
116 |
[userDefaults setDouble:[[NSDate date] timeIntervalSince1970] forKey:kAppiraterLaunchDate];
|
|
117 |
[userDefaults setInteger:1 forKey:kAppiraterLaunchCount];
|
|
118 |
[userDefaults setBool:NO forKey:kAppiraterRatedCurrentVersion];
|
|
119 |
[userDefaults setBool:NO forKey:kAppiraterDeclinedToRate];
|
|
120 |
}
|
|
121 |
|
|
122 |
[userDefaults synchronize];
|
|
123 |
if (!willShowPrompt)
|
|
124 |
[self autorelease];
|
|
125 |
|
|
126 |
[pool release];
|
|
127 |
}
|
|
128 |
|
|
129 |
-(void) showPrompt {
|
|
130 |
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:APPIRATER_MESSAGE_TITLE
|
|
131 |
message:APPIRATER_MESSAGE
|
|
132 |
delegate:self
|
|
133 |
cancelButtonTitle:APPIRATER_CANCEL_BUTTON
|
|
134 |
otherButtonTitles:APPIRATER_RATE_BUTTON, APPIRATER_RATE_LATER, nil];
|
|
135 |
[alertView show];
|
|
136 |
[alertView release];
|
|
137 |
}
|
|
138 |
|
|
139 |
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger) buttonIndex {
|
|
140 |
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
|
|
141 |
|
|
142 |
switch (buttonIndex) {
|
|
143 |
case 0:
|
|
144 |
// they don't want to rate it
|
|
145 |
[userDefaults setBool:YES forKey:kAppiraterDeclinedToRate];
|
|
146 |
break;
|
|
147 |
case 1:
|
|
148 |
// they want to rate it
|
|
149 |
[[UIApplication sharedApplication] openURL:
|
|
150 |
[NSURL URLWithString:[templateReviewURL stringByReplacingOccurrencesOfString:@"APP_ID" withString:[NSString stringWithFormat:@"%d", APPIRATER_APP_ID]]]];
|
|
151 |
|
|
152 |
[userDefaults setBool:YES forKey:kAppiraterRatedCurrentVersion];
|
|
153 |
break;
|
|
154 |
case 2:
|
|
155 |
// remind them later
|
|
156 |
break;
|
|
157 |
default:
|
|
158 |
break;
|
|
159 |
}
|
|
160 |
|
|
161 |
[userDefaults synchronize];
|
|
162 |
|
|
163 |
[self release];
|
|
164 |
}
|
|
165 |
|
|
166 |
@end
|