author | nemo |
Fri, 07 Oct 2011 17:29:00 -0400 | |
changeset 6091 | 77b826a8f996 |
parent 6078 | 8c0cc07731e5 |
child 6246 | 6b2d19ed521a |
permissions | -rw-r--r-- |
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> |
|
6078
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5483
diff
changeset
|
40 |
#import "ServerSetup.h" |
4504 | 41 |
|
42 |
NSString *const kAppiraterLaunchDate = @"kAppiraterLaunchDate"; |
|
43 |
NSString *const kAppiraterLaunchCount = @"kAppiraterLaunchCount"; |
|
44 |
NSString *const kAppiraterCurrentVersion = @"kAppiraterCurrentVersion"; |
|
45 |
NSString *const kAppiraterRatedCurrentVersion = @"kAppiraterRatedCurrentVersion"; |
|
46 |
NSString *const kAppiraterDeclinedToRate = @"kAppiraterDeclinedToRate"; |
|
47 |
||
48 |
NSString *templateReviewURL = @"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=APP_ID&onlyLatestVersion=true&pageNumber=0&sortOrdering=1&type=Purple+Software"; |
|
49 |
||
50 |
@implementation Appirater |
|
51 |
||
52 |
+(void) appLaunched { |
|
53 |
Appirater *appirater = [[Appirater alloc] init]; |
|
54 |
[NSThread detachNewThreadSelector:@selector(appLaunchedHandler) toTarget:appirater withObject:nil]; |
|
55 |
} |
|
56 |
||
57 |
-(void) appLaunchedHandler { |
|
58 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
|
59 |
||
60 |
if (APPIRATER_DEBUG) { |
|
61 |
[self performSelectorOnMainThread:@selector(showPrompt) withObject:nil waitUntilDone:NO]; |
|
62 |
return; |
|
63 |
} |
|
64 |
||
65 |
BOOL willShowPrompt = NO; |
|
66 |
||
67 |
// get the app's version |
|
68 |
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey]; |
|
69 |
||
70 |
// get the version number that we've been tracking |
|
71 |
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; |
|
72 |
NSString *trackingVersion = [userDefaults stringForKey:kAppiraterCurrentVersion]; |
|
73 |
if (trackingVersion == nil) { |
|
74 |
trackingVersion = version; |
|
75 |
[userDefaults setObject:version forKey:kAppiraterCurrentVersion]; |
|
76 |
} |
|
77 |
||
78 |
if (APPIRATER_DEBUG) |
|
79 |
DLog(@"APPIRATER Tracking version: %@", trackingVersion); |
|
80 |
||
81 |
if ([trackingVersion isEqualToString:version]) { |
|
82 |
// get the launch date |
|
83 |
NSTimeInterval timeInterval = [userDefaults doubleForKey:kAppiraterLaunchDate]; |
|
84 |
if (timeInterval == 0) { |
|
85 |
timeInterval = [[NSDate date] timeIntervalSince1970]; |
|
86 |
[userDefaults setDouble:timeInterval forKey:kAppiraterLaunchDate]; |
|
87 |
} |
|
88 |
||
89 |
NSTimeInterval secondsSinceLaunch = [[NSDate date] timeIntervalSinceDate:[NSDate dateWithTimeIntervalSince1970:timeInterval]]; |
|
90 |
double secondsUntilPrompt = 60 * 60 * 24 * DAYS_UNTIL_PROMPT; |
|
91 |
||
92 |
// get the launch count |
|
93 |
int launchCount = [userDefaults integerForKey:kAppiraterLaunchCount]; |
|
94 |
launchCount++; |
|
95 |
[userDefaults setInteger:launchCount forKey:kAppiraterLaunchCount]; |
|
96 |
if (APPIRATER_DEBUG) |
|
97 |
NSLog(@"APPIRATER Launch count: %d", launchCount); |
|
98 |
||
99 |
// have they previously declined to rate this version of the app? |
|
100 |
BOOL declinedToRate = [userDefaults boolForKey:kAppiraterDeclinedToRate]; |
|
101 |
||
102 |
// have they already rated the app? |
|
103 |
BOOL ratedApp = [userDefaults boolForKey:kAppiraterRatedCurrentVersion]; |
|
104 |
||
105 |
if (secondsSinceLaunch > secondsUntilPrompt && |
|
106 |
launchCount > LAUNCHES_UNTIL_PROMPT && |
|
107 |
!declinedToRate && |
|
108 |
!ratedApp) { |
|
6078
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5483
diff
changeset
|
109 |
if ([ServerSetup isNetworkReachable]) { // check if they can reach the app store |
4504 | 110 |
willShowPrompt = YES; |
111 |
[self performSelectorOnMainThread:@selector(showPrompt) withObject:nil waitUntilDone:NO]; |
|
112 |
} |
|
113 |
} |
|
114 |
} else { |
|
115 |
// it's a new version of the app, so restart tracking |
|
116 |
[userDefaults setObject:version forKey:kAppiraterCurrentVersion]; |
|
117 |
[userDefaults setDouble:[[NSDate date] timeIntervalSince1970] forKey:kAppiraterLaunchDate]; |
|
118 |
[userDefaults setInteger:1 forKey:kAppiraterLaunchCount]; |
|
119 |
[userDefaults setBool:NO forKey:kAppiraterRatedCurrentVersion]; |
|
120 |
[userDefaults setBool:NO forKey:kAppiraterDeclinedToRate]; |
|
121 |
} |
|
122 |
||
123 |
[userDefaults synchronize]; |
|
124 |
if (!willShowPrompt) |
|
125 |
[self autorelease]; |
|
126 |
||
127 |
[pool release]; |
|
128 |
} |
|
129 |
||
130 |
-(void) showPrompt { |
|
131 |
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:APPIRATER_MESSAGE_TITLE |
|
132 |
message:APPIRATER_MESSAGE |
|
133 |
delegate:self |
|
134 |
cancelButtonTitle:APPIRATER_CANCEL_BUTTON |
|
135 |
otherButtonTitles:APPIRATER_RATE_BUTTON, APPIRATER_RATE_LATER, nil]; |
|
136 |
[alertView show]; |
|
137 |
[alertView release]; |
|
138 |
} |
|
139 |
||
140 |
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger) buttonIndex { |
|
141 |
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; |
|
142 |
||
143 |
switch (buttonIndex) { |
|
144 |
case 0: |
|
145 |
// they don't want to rate it |
|
146 |
[userDefaults setBool:YES forKey:kAppiraterDeclinedToRate]; |
|
147 |
break; |
|
148 |
case 1: |
|
149 |
// they want to rate it |
|
150 |
[[UIApplication sharedApplication] openURL: |
|
151 |
[NSURL URLWithString:[templateReviewURL stringByReplacingOccurrencesOfString:@"APP_ID" withString:[NSString stringWithFormat:@"%d", APPIRATER_APP_ID]]]]; |
|
152 |
||
153 |
[userDefaults setBool:YES forKey:kAppiraterRatedCurrentVersion]; |
|
154 |
break; |
|
155 |
case 2: |
|
156 |
// remind them later |
|
157 |
break; |
|
158 |
default: |
|
159 |
break; |
|
160 |
} |
|
161 |
||
162 |
[userDefaults synchronize]; |
|
163 |
||
164 |
[self release]; |
|
165 |
} |
|
166 |
||
167 |
@end |