// // Geopher_LiteAppDelegate.m // Geopher Lite // // Created by Jeremy on 7/5/08. // Copyright Stone Software 2008. All rights reserved. // #import "Geopher_LiteAppDelegate.h" #import "RootViewController.h" #import "LocatorModel.h" Geopher_LiteAppDelegate* g_AppDelegate = nil; @implementation Geopher_LiteAppDelegate @synthesize window; @synthesize rootViewController; static BOOL isInitialized = NO; - (void) initialize { if (isInitialized == NO) { // status bar style [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque; [window addSubview:[rootViewController view]]; [window makeKeyAndVisible]; g_AppDelegate = self; // for simplicity, give the app delegate a global for more access from other controllers. isInitialized = YES; // make sure to only do this once, even though we call it possibly in multiple instances. } } - (void)applicationDidFinishLaunching:(UIApplication *)application { [self initialize]; } // when we are going away, save our data. - (void)applicationWillTerminate:(UIApplication *)application { // turn back on the idle timer. [[UIApplication sharedApplication] setIdleTimerDisabled:NO]; } - (void) StopAlert:(NSString*) alertMessage buttonName:(NSString*)buttonName { NSString *message = [NSString stringWithString:alertMessage]; UIAlertView *openURLAlert = [[UIAlertView alloc] initWithTitle:@"handleOpenURL:" message:message delegate:nil cancelButtonTitle:buttonName otherButtonTitles:nil]; [openURLAlert show]; [openURLAlert release]; } - (void) savePersistentData { if (rootViewController != nil && rootViewController.redirect == nil) // don't save coordinates if we are using the redirect information [rootViewController savePersistentData]; else if (rootViewController == nil) NSLog(@"rootViewController is nil in savePersisentData!"); } - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { if (!url) { // The URL is nil. There's nothing more to do. return NO; } NSString *URLString = [url absoluteString]; if (!URLString) { // The URL's absoluteString is nil. There's nothing more to do. return NO; } NSString* taskName = [[url resourceSpecifier] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSLog(@"%s", taskName); // parse the incoming URL NSRange latRange = [taskName rangeOfString:kLatURLPortion]; NSRange lonRange = [taskName rangeOfString:kLonURLPortion]; NSRange waypointRange = [taskName rangeOfString:kWaypointURLPortion]; NSRange redirectRange = [taskName rangeOfString:kRedirectURLPortion]; if (NSNotFound != latRange.location && NSNotFound != lonRange.location) { // init the app [self initialize]; // currently the URL is expected in this format: // geopherlite://setTarget;lat=12.345;lon=6.789;waypoint=GC12345;redirect=http://www.yahoo.com double targetLat = [[taskName substringFromIndex:latRange.location + latRange.length] doubleValue]; double targetLon = [[taskName substringFromIndex:lonRange.location + lonRange.length] doubleValue]; NSString* waypoint = nil; NSString* redirect = nil; if (waypointRange.location != NSNotFound) { waypoint = [NSString stringWithString:[taskName substringFromIndex:waypointRange.location + waypointRange.length]]; // now just in case there is more on the end of the string, chop it off at the next semi-colon. NSRange endOfStr = [waypoint rangeOfString:@";"]; if (endOfStr.location != NSNotFound) waypoint = [waypoint substringToIndex:endOfStr.location]; } if (redirectRange.location != NSNotFound) { // this should always be last since we don't have any way of parsing to the next "chunk" in the URL -- for maximum flexibility for the caller. redirect = [NSString stringWithString:[taskName substringFromIndex:redirectRange.location + redirectRange.length]]; } CLLocation* loc = [[[CLLocation alloc] initWithLatitude:targetLat longitude:targetLon] autorelease]; [[LocatorModel sharedInstance] setTargetLocation: loc]; if (waypoint != nil) [rootViewController updateWaypoint:waypoint]; if (redirect != nil) { [rootViewController updateRedirect:redirect]; // this code is added to call the initialize function very last, after everything else is initialized. Apparently the // ApplicationDidLaunch method does not get called if URL linking invokes the application. Moved the code into this function. //[rootViewController performSelector:@selector(updateRedirect) withObject:redirect afterDelay:0.0]; } // send an update message after 0.1 seconds, to make sure everything else gets completed. Much easier than the timer that is commented out below. // [rootViewController performSelector:@selector(updateMainView) withObject:nil afterDelay:0.1]; NSTimer* theTimer = [[NSTimer alloc] initWithFireDate:[NSDate date] interval:0.8 target:rootViewController selector:@selector(updateMainView) userInfo:nil repeats:NO]; if ([theTimer isValid] == NO) NSLog(@"Invalid Timer!"); [[NSRunLoop currentRunLoop] addTimer:theTimer forMode:NSDefaultRunLoopMode]; [theTimer release]; } else { // [self StopAlert:[NSString stringWithFormat:@"Unable to find a good target from the following URL data: %@", taskName] buttonName:@"OK"]; return NO; } return YES; } - (void)dealloc { [self savePersistentData]; [rootViewController release]; [window release]; [super dealloc]; } //- (void)applicationWillTerminate:(UIApplication *)application //{ // //} @end