// // GeopherAppDelegate.m // Geopher // // Created by Jeremy on 3/8/11. // Copyright 2011 Stone Software. All rights reserved. // #import "GeopherAppDelegate.h" #import "CompassViewController.h" #import "Reachability.h" #import "textConverter.h" #import "LocatorModel.h" BOOL NetworkIsReachable() { BOOL retVal = NO; [[Reachability sharedReachability] setHostName:@"www.opencaching.com"]; if ([[Reachability sharedReachability] internetConnectionStatus] != NotReachable) retVal = YES; return retVal; } @implementation GeopherAppDelegate @synthesize window; @synthesize compassViewController; #pragma mark settings #pragma mark - // load the persistent data off disk and return it as an NSDictionary - (NSDictionary*) readPersistentDataFile { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:kAppPlistFilename]; return [NSDictionary dictionaryWithContentsOfFile:finalPath]; } - (BOOL) writePersistentDataFile:(NSDictionary*)inDict { if (inDict == nil) return NO; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:kAppPlistFilename]; return [inDict writeToFile:finalPath atomically:YES]; } - (BOOL) setPersistentDataKey:(NSString*)inKey withValue:(id)inValue { NSDictionary* dict = [self readPersistentDataFile]; [dict setValue:inValue forKey:inKey]; return [self writePersistentDataFile:dict]; } // plist load functionality - (BOOL) loadPersistentData { BOOL retVal = NO; NSDictionary* outlineData = [[self readPersistentDataFile] retain]; // plist if (outlineData != nil) { if (compassViewController.redirect == nil) { double latitude; double longitude; // init the target coordinates NSString* str1 = [outlineData valueForKey:kTargetLatitudeKey]; NSString* str2 = [outlineData valueForKey:kTargetLongitudeKey]; if (str1 == nil) str1 = [[[NSString alloc] init] autorelease]; if (str2 == nil) str2 = [[[NSString alloc] init] autorelease]; textConverter* convLat = [[[textConverter alloc] initWithString:str1] autorelease]; textConverter* convLon = [[[textConverter alloc] initWithString:str2] autorelease]; [convLat decimalFromString:&latitude]; [convLon decimalFromString:&longitude]; CLLocation* loc = [[[CLLocation alloc] initWithLatitude:latitude longitude:longitude] autorelease]; [[LocatorModel sharedInstance] setTargetLocation: loc]; // now init the last current coordinates we knew about. str1 = [outlineData valueForKey:kCurrentLatitudeKey]; str2 = [outlineData valueForKey:kCurrentLongitudeKey]; if (str1 == nil) str1 = [[[NSString alloc] init] autorelease]; if (str2 == nil) str2 = [[[NSString alloc] init] autorelease]; convLat = [[[textConverter alloc] initWithString:str1] autorelease]; convLon = [[[textConverter alloc] initWithString:str2] autorelease]; [convLat decimalFromString:&latitude]; [convLon decimalFromString:&longitude]; loc = [[[CLLocation alloc] initWithLatitude:latitude longitude:longitude] autorelease]; [[LocatorModel sharedInstance] setCurrentLocation: loc]; } // update the waypoint text field compassViewController.flipsideViewController.updateWaypointString = [[outlineData valueForKey:kWaypointKey] retain]; // update the fields [[LocatorModel sharedInstance] update]; [outlineData release]; retVal = YES; } return retVal; } // plist save functionality - (void) savePersistentData { if (compassViewController.redirect != nil) // don't save coordinates if we are using the redirect information return; NSMutableDictionary* dict = [NSMutableDictionary dictionary]; // write the plist [dict setObject:[NSString stringWithFormat:@"%lu", kPListVersionNumber] forKey:kPListVersion]; [dict setObject:[[LocatorModel sharedInstance] targetLatitudeText: NO] forKey:kTargetLatitudeKey]; [dict setObject:[[LocatorModel sharedInstance] targetLongitudeText: NO] forKey:kTargetLongitudeKey]; [dict setObject:[[LocatorModel sharedInstance] currentLatitudeText: NO] forKey:kCurrentLatitudeKey]; [dict setObject:[[LocatorModel sharedInstance] currentLongitudeText: NO] forKey:kCurrentLongitudeKey]; if (compassViewController.flipsideViewController != nil) // this should never get called when this is nil, but just in case... [dict setObject:[NSString stringWithString:compassViewController.flipsideViewController.cacheIDTextView.text] forKey:kWaypointKey]; else NSLog(@"flipsieViewController nil in savePersistentData!"); // write the web archive // BOOL result = [NSKeyedArchiver archiveRootObject:((FlipsideView*)compassViewController.flipsideViewController.view).webView toFile:webArchive]; BOOL result = [compassViewController.flipsideViewController ArchiveWebView]; if (NO == result) { NSLog(@"Error writing web archive to file!"); } if (NO == [self writePersistentDataFile:dict]) { NSLog(@"Error writing plist to file!"); } } // system prefs - (void) initSystemPreferences { NSString *testValue = [[NSUserDefaults standardUserDefaults] stringForKey:kOutdoorMode]; if (testValue == nil) { // since no default values have been set (i.e. no preferences file created), create it here NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:1], kPreferenceSkinKey, [NSNumber numberWithInt:1], kMeasurementKey, nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults]; [[NSUserDefaults standardUserDefaults] synchronize]; [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kHideFoundCaches]; [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kAutoLoadSearchKey]; [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kUseInternalMapsKey]; [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:kMagneticCompassKey]; [[NSUserDefaults standardUserDefaults] setBool:NO forKey:kOutdoorMode]; } } #pragma mark - #pragma mark Application lifecycle - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // Set the main view controller as the window's root view controller and display. self.window.rootViewController = self.compassViewController; [self.window makeKeyAndVisible]; return YES; } - (void)applicationWillResignActive:(UIApplication *)application { /* Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. */ } - (void)applicationDidEnterBackground:(UIApplication *)application { /* Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. If your application supports background execution, called instead of applicationWillTerminate: when the user quits. */ } - (void)applicationWillEnterForeground:(UIApplication *)application { /* Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background. */ } - (void)applicationDidBecomeActive:(UIApplication *)application { /* Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. */ } - (void)applicationWillTerminate:(UIApplication *)application { /* Called when the application is about to terminate. See also applicationDidEnterBackground:. */ } #pragma mark - #pragma mark Memory management - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application { /* Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later. */ } - (void)dealloc { [compassViewController release]; [window release]; [super dealloc]; } @end