project_files/HedgewarsMobile/Classes/Appirater.m
author koda
Thu, 22 Sep 2011 01:02:57 +0200
changeset 5984 6fd40d866342
parent 5483 fc755bb8096d
child 6078 8c0cc07731e5
permissions -rw-r--r--
create the ios schemes programmatically
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>
5483
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 4504
diff changeset
    40
#import "CommodityFunctions.h"
4504
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
NSString *const kAppiraterLaunchDate            = @"kAppiraterLaunchDate";
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    43
NSString *const kAppiraterLaunchCount           = @"kAppiraterLaunchCount";
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    44
NSString *const kAppiraterCurrentVersion        = @"kAppiraterCurrentVersion";
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    45
NSString *const kAppiraterRatedCurrentVersion   = @"kAppiraterRatedCurrentVersion";
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    46
NSString *const kAppiraterDeclinedToRate        = @"kAppiraterDeclinedToRate";
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
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
    49
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    50
@implementation Appirater
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
+(void) appLaunched {
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    53
    Appirater *appirater = [[Appirater alloc] init];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    54
    [NSThread detachNewThreadSelector:@selector(appLaunchedHandler) toTarget:appirater withObject:nil];
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
-(void) appLaunchedHandler {
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    58
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    59
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    60
    if (APPIRATER_DEBUG) {
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    61
        [self performSelectorOnMainThread:@selector(showPrompt) withObject:nil waitUntilDone:NO];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    62
        return;
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
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    65
    BOOL willShowPrompt = NO;
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    66
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    67
    // get the app's version
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    68
    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey];
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
    // get the version number that we've been tracking
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    71
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    72
    NSString *trackingVersion = [userDefaults stringForKey:kAppiraterCurrentVersion];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    73
    if (trackingVersion == nil) {
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    74
        trackingVersion = version;
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    75
        [userDefaults setObject:version forKey:kAppiraterCurrentVersion];
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
    
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    78
    if (APPIRATER_DEBUG)
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    79
        DLog(@"APPIRATER Tracking version: %@", trackingVersion);
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
    if ([trackingVersion isEqualToString:version]) {
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    82
        // get the launch date
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    83
        NSTimeInterval timeInterval = [userDefaults doubleForKey:kAppiraterLaunchDate];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    84
        if (timeInterval == 0) {
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    85
            timeInterval = [[NSDate date] timeIntervalSince1970];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    86
            [userDefaults setDouble:timeInterval forKey:kAppiraterLaunchDate];
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
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    89
        NSTimeInterval secondsSinceLaunch = [[NSDate date] timeIntervalSinceDate:[NSDate dateWithTimeIntervalSince1970:timeInterval]];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    90
        double secondsUntilPrompt = 60 * 60 * 24 * DAYS_UNTIL_PROMPT;
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    91
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    92
        // get the launch count
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    93
        int launchCount = [userDefaults integerForKey:kAppiraterLaunchCount];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    94
        launchCount++;
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    95
        [userDefaults setInteger:launchCount forKey:kAppiraterLaunchCount];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    96
        if (APPIRATER_DEBUG)
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    97
            NSLog(@"APPIRATER Launch count: %d", launchCount);
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    98
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
    99
        // 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
   100
        BOOL declinedToRate = [userDefaults boolForKey:kAppiraterDeclinedToRate];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   101
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   102
        // have they already rated the app?
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   103
        BOOL ratedApp = [userDefaults boolForKey:kAppiraterRatedCurrentVersion];
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
        if (secondsSinceLaunch > secondsUntilPrompt &&
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   106
             launchCount > LAUNCHES_UNTIL_PROMPT &&
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   107
             !declinedToRate &&
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   108
             !ratedApp) {
5483
fc755bb8096d ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
koda
parents: 4504
diff changeset
   109
            if (isNetworkReachable()) {	// check if they can reach the app store
4504
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   110
                willShowPrompt = YES;
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   111
                [self performSelectorOnMainThread:@selector(showPrompt) withObject:nil waitUntilDone:NO];
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
        }
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   114
    } else {
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   115
        // 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
   116
        [userDefaults setObject:version forKey:kAppiraterCurrentVersion];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   117
        [userDefaults setDouble:[[NSDate date] timeIntervalSince1970] forKey:kAppiraterLaunchDate];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   118
        [userDefaults setInteger:1 forKey:kAppiraterLaunchCount];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   119
        [userDefaults setBool:NO forKey:kAppiraterRatedCurrentVersion];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   120
        [userDefaults setBool:NO forKey:kAppiraterDeclinedToRate];
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
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   123
    [userDefaults synchronize];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   124
    if (!willShowPrompt)
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   125
        [self autorelease];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   126
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   127
    [pool release];
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
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   130
-(void) showPrompt {
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   131
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:APPIRATER_MESSAGE_TITLE
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   132
                                                        message:APPIRATER_MESSAGE
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   133
                                                       delegate:self
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   134
                                              cancelButtonTitle:APPIRATER_CANCEL_BUTTON
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   135
                                              otherButtonTitles:APPIRATER_RATE_BUTTON, APPIRATER_RATE_LATER, nil];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   136
    [alertView show];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   137
    [alertView release];
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
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   140
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger) buttonIndex {
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   141
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   142
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   143
    switch (buttonIndex) {
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   144
        case 0:
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   145
            // they don't want to rate it
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   146
            [userDefaults setBool:YES forKey:kAppiraterDeclinedToRate];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   147
            break;
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   148
        case 1:
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   149
            // they want to rate it
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   150
            [[UIApplication sharedApplication] openURL:
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   151
             [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
   152
            
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   153
            [userDefaults setBool:YES forKey:kAppiraterRatedCurrentVersion];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   154
            break;
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   155
        case 2:
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   156
            // remind them later
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   157
            break;
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   158
        default:
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   159
            break;
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
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   162
    [userDefaults synchronize];
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   163
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   164
    [self release];
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
8906b2409d97 add the appirater class for getting more positive reviews
koda
parents:
diff changeset
   167
@end