project_files/HedgewarsMobile/Classes/Appirater.m
author koda
Fri, 11 Nov 2011 00:23:09 +0100
changeset 6319 b57a37a94ad3
parent 6246 6b2d19ed521a
child 6832 fae8fd118da9
permissions -rw-r--r--
restore a lost view message
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
/*
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
     2
 This file is part of Appirater.
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
 */
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    28
/*
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    29
 * Appirater.m
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    30
 * appirater
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    31
 *
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    32
 * Created by Arash Payan on 9/5/09.
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    33
 * http://arashpayan.com
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    34
 * Copyright 2010 Arash Payan. All rights reserved.
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    35
 */
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    36
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    37
#import "Appirater.h"
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    38
#import <SystemConfiguration/SCNetworkReachability.h>
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    39
#import <netinet/in.h>
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    40
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    41
NSString *const kAppiraterLaunchDate            = @"kAppiraterLaunchDate";
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    42
NSString *const kAppiraterLaunchCount           = @"kAppiraterLaunchCount";
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    43
NSString *const kAppiraterCurrentVersion        = @"kAppiraterCurrentVersion";
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    44
NSString *const kAppiraterRatedCurrentVersion   = @"kAppiraterRatedCurrentVersion";
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    45
NSString *const kAppiraterDeclinedToRate        = @"kAppiraterDeclinedToRate";
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    46
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    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";
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
@implementation Appirater
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    50
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    51
+(void) appLaunched {
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    52
    Appirater *appirater = [[Appirater alloc] init];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    53
    [NSThread detachNewThreadSelector:@selector(appLaunchedHandler) toTarget:appirater withObject:nil];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    54
}
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
-(void) appLaunchedHandler {
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    57
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
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
    if (APPIRATER_DEBUG) {
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    60
        [self performSelectorOnMainThread:@selector(showPrompt) withObject:nil waitUntilDone:NO];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    61
        return;
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    62
    }
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    63
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    64
    BOOL willShowPrompt = NO;
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    65
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    66
    // get the app's version
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    67
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey];
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
    // get the version number that we've been tracking
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    70
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    71
    NSString *trackingVersion = [userDefaults stringForKey:kAppiraterCurrentVersion];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    72
    if (trackingVersion == nil) {
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    73
        trackingVersion = version;
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    74
        [userDefaults setObject:version forKey:kAppiraterCurrentVersion];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    75
    }
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    76
    
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    77
    if (APPIRATER_DEBUG)
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    78
        DLog(@"APPIRATER Tracking version: %@", trackingVersion);
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
    if ([trackingVersion isEqualToString:version]) {
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    81
        // get the launch date
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    82
        NSTimeInterval timeInterval = [userDefaults doubleForKey:kAppiraterLaunchDate];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    83
        if (timeInterval == 0) {
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    84
            timeInterval = [[NSDate date] timeIntervalSince1970];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    85
            [userDefaults setDouble:timeInterval forKey:kAppiraterLaunchDate];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    86
        }
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    87
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    88
        NSTimeInterval secondsSinceLaunch = [[NSDate date] timeIntervalSinceDate:[NSDate dateWithTimeIntervalSince1970:timeInterval]];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    89
        double secondsUntilPrompt = 60 * 60 * 24 * DAYS_UNTIL_PROMPT;
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
        // get the launch count
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    92
        int launchCount = [userDefaults integerForKey:kAppiraterLaunchCount];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    93
        launchCount++;
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    94
        [userDefaults setInteger:launchCount forKey:kAppiraterLaunchCount];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    95
        if (APPIRATER_DEBUG)
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    96
            NSLog(@"APPIRATER Launch count: %d", launchCount);
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    97
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    98
        // 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
    99
        BOOL declinedToRate = [userDefaults boolForKey:kAppiraterDeclinedToRate];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   100
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   101
        // have they already rated the app?
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   102
        BOOL ratedApp = [userDefaults boolForKey:kAppiraterRatedCurrentVersion];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   103
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   104
        if (secondsSinceLaunch > secondsUntilPrompt &&
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   105
             launchCount > LAUNCHES_UNTIL_PROMPT &&
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   106
             !declinedToRate &&
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   107
             !ratedApp) {
6246
6b2d19ed521a just moving ios server stuff around
koda
parents: 6078
diff changeset
   108
            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
   109
                willShowPrompt = YES;
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   110
                [self performSelectorOnMainThread:@selector(showPrompt) withObject:nil waitUntilDone:NO];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   111
            }
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   112
        }
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   113
    } else {
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   114
        // 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
   115
        [userDefaults setObject:version forKey:kAppiraterCurrentVersion];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   116
        [userDefaults setDouble:[[NSDate date] timeIntervalSince1970] forKey:kAppiraterLaunchDate];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   117
        [userDefaults setInteger:1 forKey:kAppiraterLaunchCount];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   118
        [userDefaults setBool:NO forKey:kAppiraterRatedCurrentVersion];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   119
        [userDefaults setBool:NO forKey:kAppiraterDeclinedToRate];
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
    [userDefaults synchronize];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   123
    if (!willShowPrompt)
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   124
        [self autorelease];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   125
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   126
    [pool release];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   127
}
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   128
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   129
-(void) showPrompt {
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   130
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:APPIRATER_MESSAGE_TITLE
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   131
                                                        message:APPIRATER_MESSAGE
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   132
                                                       delegate:self
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   133
                                              cancelButtonTitle:APPIRATER_CANCEL_BUTTON
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   134
                                              otherButtonTitles:APPIRATER_RATE_BUTTON, APPIRATER_RATE_LATER, nil];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   135
    [alertView show];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   136
    [alertView release];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   137
}
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   138
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   139
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger) buttonIndex {
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   140
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   141
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   142
    switch (buttonIndex) {
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   143
        case 0:
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   144
            // they don't want to rate it
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   145
            [userDefaults setBool:YES forKey:kAppiraterDeclinedToRate];
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 1:
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   148
            // they want to rate it
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   149
            [[UIApplication sharedApplication] openURL:
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   150
             [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
   151
            
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   152
            [userDefaults setBool:YES forKey:kAppiraterRatedCurrentVersion];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   153
            break;
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   154
        case 2:
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   155
            // remind them later
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   156
            break;
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   157
        default:
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   158
            break;
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   159
    }
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   160
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   161
    [userDefaults synchronize];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   162
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   163
    [self release];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   164
}
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   165
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   166
@end