// // Geopher_OpenCachingAppDelegate.m // Geopher OpenCaching // // Created by Jeremy on 12/9/10. // Copyright 2010 Stone Software. All rights reserved. // #import "Geopher_OpenCachingAppDelegate.h" #import "LocatorModel.h" #import "CompassViewController.h" #import "Reachability.h" #import "SavedCachesController.h" Geopher_OpenCachingAppDelegate* GetAppDelegate() { return (Geopher_OpenCachingAppDelegate*)[[UIApplication sharedApplication] delegate]; } BOOL NetworkIsReachable() { BOOL retVal = NO; [[Reachability sharedReachability] setHostName:@"www.opencaching.com"]; if ([[Reachability sharedReachability] internetConnectionStatus] != NotReachable) retVal = YES; return retVal; } #pragma mark - @implementation Geopher_OpenCachingAppDelegate @synthesize window; @synthesize tabBarController; #pragma mark - #pragma mark Application lifecycle - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [self loadPrefs]; // Add the tab bar controller's view to the window and display. [self.window addSubview:tabBarController.view]; [self.window makeKeyAndVisible]; [[LocatorModel sharedInstance] currentLocation]; // start up the GPS 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. */ [[UIApplication sharedApplication] setIdleTimerDisabled:NO]; // default the target screen to not go to sleep by default } - (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. */ [[UIApplication sharedApplication] setIdleTimerDisabled:NO]; // default the target screen to not go to sleep by default } - (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:. */ [[UIApplication sharedApplication] setIdleTimerDisabled:NO]; // default the target screen to not go to sleep by default } #pragma mark - #pragma mark Delegated Tasks - (void) setNewTargetWithLatitude:(NSString*)lat longitude:(NSString*)lon { if (lat && lon) { CLLocation* loc = [[CLLocation alloc] initWithLatitude:[lat doubleValue] longitude:[lon doubleValue]]; [[LocatorModel sharedInstance] setTargetLocation:loc]; [loc release]; } // now change to the navigation tab. Apparently we have to fake it as there's no good way to do this programmatically. That I can find anyhow. // first find the navigation view. CompassViewController* navController = nil; for (UIViewController* contr in [self.tabBarController viewControllers]) { if ([contr isKindOfClass:[CompassViewController class]]) navController = (CompassViewController*)contr; } if (navController) { CGContextRef context = UIGraphicsGetCurrentContext(); [UIView beginAnimations:nil context:context]; [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:[self.tabBarController.selectedViewController view] cache:YES]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationDuration:1.0]; [UIView transitionFromView:self.tabBarController.selectedViewController.view toView:navController.view duration:0.75 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) { [self.tabBarController setSelectedIndex:[self.tabBarController.viewControllers indexOfObject:navController]]; }]; [UIView commitAnimations]; } } - (CompassViewController*) compassScreen { NSArray* views = [tabBarController viewControllers]; for (id contr in views) { if ([contr isKindOfClass:[CompassViewController class]]) return contr; } return nil; } - (void) updateCompassScreen { [[self compassScreen] update]; } #pragma mark - #pragma mark Preferences - (NSString*) prefsFilePath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths lastObject]; NSString* prefsFile = [documentsDirectory stringByAppendingPathComponent:@"OpenCachingPrefs.plist"]; return prefsFile; } - (NSMutableDictionary*) appPrefs { if (!appPrefs) [self loadPrefs]; return appPrefs; } - (void) loadPrefs { NSString* prefsFile = [self prefsFilePath]; appPrefs = [[NSMutableDictionary dictionaryWithContentsOfFile:prefsFile] retain]; // if the prefs don't exist set the defaults if (!appPrefs) { appPrefs = [[NSMutableDictionary alloc] initWithCapacity:10]; [appPrefs setObject:[NSNumber numberWithBool:NO] forKey:kMeasurementInMetersKey]; [appPrefs setObject:[NSNumber numberWithInt:kDefaultSearchPrefValue] forKey:kSearchResultsPrefKey]; } } - (void) savePrefs { NSString* prefsPath = [self prefsFilePath]; BOOL done = [appPrefs writeToFile:prefsPath atomically:YES]; if (!done) NSLog(@"Unable to write preferences file to path: %@", prefsPath); } #pragma mark - #pragma mark UITabBarControllerDelegate methods /* // Optional UITabBarControllerDelegate method. - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { } */ /* // Optional UITabBarControllerDelegate method. - (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed { } */ #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 { [tabBarController release]; [window release]; [super dealloc]; } @end