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