project_files/HedgewarsMobile/Classes/Appirater.m
changeset 5483 fc755bb8096d
parent 4504 8906b2409d97
child 6078 8c0cc07731e5
equal deleted inserted replaced
5482:c047e70c53f4 5483:fc755bb8096d
    35  */
    35  */
    36 
    36 
    37 #import "Appirater.h"
    37 #import "Appirater.h"
    38 #import <SystemConfiguration/SCNetworkReachability.h>
    38 #import <SystemConfiguration/SCNetworkReachability.h>
    39 #import <netinet/in.h>
    39 #import <netinet/in.h>
       
    40 #import "CommodityFunctions.h"
    40 
    41 
    41 NSString *const kAppiraterLaunchDate            = @"kAppiraterLaunchDate";
    42 NSString *const kAppiraterLaunchDate            = @"kAppiraterLaunchDate";
    42 NSString *const kAppiraterLaunchCount           = @"kAppiraterLaunchCount";
    43 NSString *const kAppiraterLaunchCount           = @"kAppiraterLaunchCount";
    43 NSString *const kAppiraterCurrentVersion        = @"kAppiraterCurrentVersion";
    44 NSString *const kAppiraterCurrentVersion        = @"kAppiraterCurrentVersion";
    44 NSString *const kAppiraterRatedCurrentVersion   = @"kAppiraterRatedCurrentVersion";
    45 NSString *const kAppiraterRatedCurrentVersion   = @"kAppiraterRatedCurrentVersion";
    45 NSString *const kAppiraterDeclinedToRate        = @"kAppiraterDeclinedToRate";
    46 NSString *const kAppiraterDeclinedToRate        = @"kAppiraterDeclinedToRate";
    46 
    47 
    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 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 @interface Appirater (hidden)
       
    50 
       
    51 -(BOOL) connectedToNetwork;
       
    52 
       
    53 @end
       
    54 
       
    55 @implementation Appirater (hidden)
       
    56 
       
    57 -(BOOL) connectedToNetwork {
       
    58     // Create zero addy
       
    59     struct sockaddr_in zeroAddress;
       
    60     bzero(&zeroAddress, sizeof(zeroAddress));
       
    61     zeroAddress.sin_len = sizeof(zeroAddress);
       
    62     zeroAddress.sin_family = AF_INET;
       
    63     
       
    64     // Recover reachability flags
       
    65     SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
       
    66     SCNetworkReachabilityFlags flags;
       
    67     
       
    68     BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
       
    69     CFRelease(defaultRouteReachability);
       
    70     
       
    71     if (!didRetrieveFlags) {
       
    72         NSLog(@"Error. Could not recover network reachability flags");
       
    73         return NO;
       
    74     }
       
    75     
       
    76     BOOL isReachable = flags & kSCNetworkFlagsReachable;
       
    77     BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
       
    78     BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;
       
    79     
       
    80     NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"];
       
    81     NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0];
       
    82     NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:self];
       
    83     
       
    84     return ((isReachable && !needsConnection) || nonWiFi) ? (testConnection ? YES : NO) : NO;
       
    85 }
       
    86 
       
    87 @end
       
    88 
       
    89 
    49 
    90 @implementation Appirater
    50 @implementation Appirater
    91 
    51 
    92 +(void) appLaunched {
    52 +(void) appLaunched {
    93     Appirater *appirater = [[Appirater alloc] init];
    53     Appirater *appirater = [[Appirater alloc] init];
   144 
   104 
   145         if (secondsSinceLaunch > secondsUntilPrompt &&
   105         if (secondsSinceLaunch > secondsUntilPrompt &&
   146              launchCount > LAUNCHES_UNTIL_PROMPT &&
   106              launchCount > LAUNCHES_UNTIL_PROMPT &&
   147              !declinedToRate &&
   107              !declinedToRate &&
   148              !ratedApp) {
   108              !ratedApp) {
   149             if ([self connectedToNetwork]) {	// check if they can reach the app store
   109             if (isNetworkReachable()) {	// check if they can reach the app store
   150                 willShowPrompt = YES;
   110                 willShowPrompt = YES;
   151                 [self performSelectorOnMainThread:@selector(showPrompt) withObject:nil waitUntilDone:NO];
   111                 [self performSelectorOnMainThread:@selector(showPrompt) withObject:nil waitUntilDone:NO];
   152             }
   112             }
   153         }
   113         }
   154     } else {
   114     } else {