ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
authorkoda
Sun, 31 Jul 2011 16:03:24 +0200
changeset 5483 fc755bb8096d
parent 5482 c047e70c53f4
child 5484 1adde4aaedea
ios: caught some leaks, merged two methods inside a fuction, postponed the rating popup
project_files/HedgewarsMobile/Classes/Appirater.m
project_files/HedgewarsMobile/Classes/CommodityFunctions.h
project_files/HedgewarsMobile/Classes/CommodityFunctions.m
project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.m
project_files/HedgewarsMobile/Classes/InGameMenuViewController.m
project_files/HedgewarsMobile/Classes/MainMenuViewController.m
project_files/HedgewarsMobile/Classes/MapConfigViewController.m
project_files/HedgewarsMobile/Classes/ServerSetup.h
project_files/HedgewarsMobile/Classes/ServerSetup.m
--- a/project_files/HedgewarsMobile/Classes/Appirater.m	Sat Jul 30 17:23:47 2011 -0400
+++ b/project_files/HedgewarsMobile/Classes/Appirater.m	Sun Jul 31 16:03:24 2011 +0200
@@ -37,6 +37,7 @@
 #import "Appirater.h"
 #import <SystemConfiguration/SCNetworkReachability.h>
 #import <netinet/in.h>
+#import "CommodityFunctions.h"
 
 NSString *const kAppiraterLaunchDate            = @"kAppiraterLaunchDate";
 NSString *const kAppiraterLaunchCount           = @"kAppiraterLaunchCount";
@@ -46,47 +47,6 @@
 
 NSString *templateReviewURL = @"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=APP_ID&onlyLatestVersion=true&pageNumber=0&sortOrdering=1&type=Purple+Software";
 
-@interface Appirater (hidden)
-
--(BOOL) connectedToNetwork;
-
-@end
-
-@implementation Appirater (hidden)
-
--(BOOL) connectedToNetwork {
-    // Create zero addy
-    struct sockaddr_in zeroAddress;
-    bzero(&zeroAddress, sizeof(zeroAddress));
-    zeroAddress.sin_len = sizeof(zeroAddress);
-    zeroAddress.sin_family = AF_INET;
-    
-    // Recover reachability flags
-    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
-    SCNetworkReachabilityFlags flags;
-    
-    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
-    CFRelease(defaultRouteReachability);
-    
-    if (!didRetrieveFlags) {
-        NSLog(@"Error. Could not recover network reachability flags");
-        return NO;
-    }
-    
-    BOOL isReachable = flags & kSCNetworkFlagsReachable;
-    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
-    BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;
-    
-    NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"];
-    NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0];
-    NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:self];
-    
-    return ((isReachable && !needsConnection) || nonWiFi) ? (testConnection ? YES : NO) : NO;
-}
-
-@end
-
-
 @implementation Appirater
 
 +(void) appLaunched {
@@ -146,7 +106,7 @@
              launchCount > LAUNCHES_UNTIL_PROMPT &&
              !declinedToRate &&
              !ratedApp) {
-            if ([self connectedToNetwork]) {	// check if they can reach the app store
+            if (isNetworkReachable()) {	// check if they can reach the app store
                 willShowPrompt = YES;
                 [self performSelectorOnMainThread:@selector(showPrompt) withObject:nil waitUntilDone:NO];
             }
--- a/project_files/HedgewarsMobile/Classes/CommodityFunctions.h	Sat Jul 30 17:23:47 2011 -0400
+++ b/project_files/HedgewarsMobile/Classes/CommodityFunctions.h	Sun Jul 31 16:03:24 2011 +0200
@@ -76,6 +76,7 @@
 UILabel *createLabelWithParams (NSString *title, CGRect frame, CGFloat borderWidth, UIColor *borderColor, UIColor *backgroundColor);
 
 CGSize PSPNGSizeFromMetaData (NSString *aFileName);
+BOOL isNetworkReachable (void);
 
 @interface NSString (extra)
 
--- a/project_files/HedgewarsMobile/Classes/CommodityFunctions.m	Sat Jul 30 17:23:47 2011 -0400
+++ b/project_files/HedgewarsMobile/Classes/CommodityFunctions.m	Sun Jul 31 16:03:24 2011 +0200
@@ -27,6 +27,8 @@
 #import <QuartzCore/QuartzCore.h>
 #import <AudioToolbox/AudioToolbox.h>
 #import <CommonCrypto/CommonDigest.h>
+#import <SystemConfiguration/SCNetworkReachability.h>
+#import <netinet/in.h>
 #import "PascalImports.h"
 #import "hwconsts.h"
 
@@ -128,6 +130,40 @@
     return theLabel;
 }
 
+BOOL isNetworkReachable (void) {
+    // Create zero addy
+    struct sockaddr_in zeroAddress;
+    bzero(&zeroAddress, sizeof(zeroAddress));
+    zeroAddress.sin_len = sizeof(zeroAddress);
+    zeroAddress.sin_family = AF_INET;
+
+    // Recover reachability flags
+    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
+    SCNetworkReachabilityFlags flags;
+
+    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
+    CFRelease(defaultRouteReachability);
+
+    if (!didRetrieveFlags) {
+        NSLog(@"Error. Could not recover network reachability flags");
+        return NO;
+    }
+
+    BOOL isReachable = flags & kSCNetworkFlagsReachable;
+    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
+    BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;
+
+    NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"];
+    NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL
+                                                 cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
+                                             timeoutInterval:20.0];
+    NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:nil];
+    BOOL testResult = testConnection ? YES : NO;
+    [testConnection release];
+
+    return ((isReachable && !needsConnection) || nonWiFi) ? testResult : NO;
+}
+
 // this routine checks for the PNG size without loading it in memory
 // https://github.com/steipete/PSFramework/blob/master/PSFramework%20Version%200.3/PhotoshopFramework/PSMetaDataFunctions.m
 CGSize PSPNGSizeFromMetaData (NSString *aFileName) {
--- a/project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.m	Sat Jul 30 17:23:47 2011 -0400
+++ b/project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.m	Sun Jul 31 16:03:24 2011 +0200
@@ -25,7 +25,6 @@
 #import "CommodityFunctions.h"
 #import "MainMenuViewController.h"
 #import "AVFoundation/AVAudioPlayer.h"
-#import "Appirater.h"
 #include <unistd.h>
 
 
@@ -97,7 +96,6 @@
 // override the direct execution of SDL_main to allow us to implement our own frontend
 -(void) postFinishLaunch {
     [[UIApplication sharedApplication] setStatusBarHidden:YES];
-    [Appirater appLaunched];
 
     self.uiwindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 
--- a/project_files/HedgewarsMobile/Classes/InGameMenuViewController.m	Sat Jul 30 17:23:47 2011 -0400
+++ b/project_files/HedgewarsMobile/Classes/InGameMenuViewController.m	Sun Jul 31 16:03:24 2011 +0200
@@ -257,7 +257,8 @@
         image = [[UIImage alloc] initWithCGImage:imageRef];
     CGImageRelease(imageRef);
 
-    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (void *)alert); // add callback for finish saving
+    // add callback for cleaning memory and removing alert
+    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (void *)alert);
 }
 
 
--- a/project_files/HedgewarsMobile/Classes/MainMenuViewController.m	Sat Jul 30 17:23:47 2011 -0400
+++ b/project_files/HedgewarsMobile/Classes/MainMenuViewController.m	Sun Jul 31 16:03:24 2011 +0200
@@ -27,6 +27,7 @@
 #import "AboutViewController.h"
 #import "SavedGamesViewController.h"
 #import "RestoreViewController.h"
+#import "Appirater.h"
 #import "ServerSetup.h"
 
 @implementation MainMenuViewController
@@ -72,7 +73,7 @@
         [[NSFileManager defaultManager] removeItemAtPath:SCHEMES_DIRECTORY() error:NULL];
     NSString *baseSchemesDir = [[NSString alloc] initWithFormat:@"%@/Settings/Schemes/",resourcesDir];
     [[NSFileManager defaultManager] copyItemAtPath:baseSchemesDir toPath:SCHEMES_DIRECTORY() error:NULL];
-    
+    [baseSchemesDir release];
 
     // WEAPONS - always overwrite
     if ([[NSFileManager defaultManager] fileExistsAtPath:WEAPONS_DIRECTORY()] == NO)
@@ -121,11 +122,15 @@
             [restored release];
         }
         [self performSelector:@selector(presentModalViewController:animated:) withObject:self.restoreViewController afterDelay:0.35];
+    } else {
+        // let's not prompt for rating when app crashed >_>
+        [Appirater appLaunched];
     }
 
+
     /*
     ServerSetup *setup = [[ServerSetup alloc] init];
-    if ([setup isNetworkReachable]) {
+    if (isNetworkReachable()) {
         DLog(@"network is reachable");
         [NSThread detachNewThreadSelector:@selector(serverProtocol)
                                  toTarget:setup
--- a/project_files/HedgewarsMobile/Classes/MapConfigViewController.m	Sat Jul 30 17:23:47 2011 -0400
+++ b/project_files/HedgewarsMobile/Classes/MapConfigViewController.m	Sun Jul 31 16:03:24 2011 +0200
@@ -387,6 +387,7 @@
         NSString *checkPath = [[NSString alloc] initWithFormat:@"%@/%@/icon.png",THEMES_DIRECTORY(),themeName];
         if ([[NSFileManager defaultManager] fileExistsAtPath:checkPath])
             [themeArray addObject:themeName];
+        [checkPath release];
     }
 
     // remove images that are too big for certain devices without loading the whole image
--- a/project_files/HedgewarsMobile/Classes/ServerSetup.h	Sat Jul 30 17:23:47 2011 -0400
+++ b/project_files/HedgewarsMobile/Classes/ServerSetup.h	Sun Jul 31 16:03:24 2011 +0200
@@ -30,6 +30,4 @@
 
 @property (nonatomic, retain) NSDictionary *systemSettings;
 
--(BOOL) isNetworkReachable;
-
 @end
--- a/project_files/HedgewarsMobile/Classes/ServerSetup.m	Sat Jul 30 17:23:47 2011 -0400
+++ b/project_files/HedgewarsMobile/Classes/ServerSetup.m	Sun Jul 31 16:03:24 2011 +0200
@@ -22,8 +22,6 @@
 #import "ServerSetup.h"
 #import "PascalImports.h"
 #import "CommodityFunctions.h"
-#import <SystemConfiguration/SCNetworkReachability.h>
-#import <netinet/in.h>
 #import "hwconsts.h"
 
 #define BUFFER_SIZE 256
@@ -43,39 +41,6 @@
     [super dealloc];
 }
 
-// reusing appirater method
--(BOOL) isNetworkReachable {
-    // Create zero addy
-    struct sockaddr_in zeroAddress;
-    bzero(&zeroAddress, sizeof(zeroAddress));
-    zeroAddress.sin_len = sizeof(zeroAddress);
-    zeroAddress.sin_family = AF_INET;
-    
-    // Recover reachability flags
-    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
-    SCNetworkReachabilityFlags flags;
-    
-    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
-    CFRelease(defaultRouteReachability);
-    
-    if (!didRetrieveFlags) {
-        NSLog(@"Error. Could not recover network reachability flags");
-        return NO;
-    }
-    
-    BOOL isReachable = flags & kSCNetworkFlagsReachable;
-    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
-    BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;
-    
-    NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"];
-    NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL
-                                                 cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
-                                             timeoutInterval:20.0];
-    NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:self];
-    
-    return ((isReachable && !needsConnection) || nonWiFi) ? (testConnection ? YES : NO) : NO;
-}
-
 -(int) sendToServer:(NSString *)command {
     NSString *message = [[NSString alloc] initWithFormat:@"%@\n\n",command];
     int result = SDLNet_TCP_Send(sd, [message UTF8String], [message length]);