Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to launch view controller from alarm notification action in iOS?

In my app i am using alarm functionality. It working fine. When i click the right button it launches my app. But i want to launch View Controller which is not an rootViewController. I am tried in searching in Google and SO but i couldn't get any idea or example.

I am looking for any example to achieve this.?

Thanks for your help guys.

EDIT

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{     
// Add the view controller's view to the window and display.
[self.window addSubview:alarmViewController.view];
[self.window makeKeyAndVisible];

application.applicationIconBadgeNumber = 0;


// Handle launching from a notification
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
    NSLog(@"Recieved Notification %@",localNotif);

    window = [[UIApplication sharedApplication] keyWindow];
    UIViewController *rootViewController = [window rootViewController];
    [rootViewController presentModalViewController:receipeViewController animated:YES];         
}


// Override point for customization after application launch.
return YES;

}

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
// Handle the notificaton when the app is running
NSLog(@"Recieved Notification %@",notif);   
//Called here as well
window = [[UIApplication sharedApplication] keyWindow];
    UIViewController *rootViewController = [window rootViewController];
    [rootViewController presentModalViewController:receipeViewController animated:YES];   
}
like image 974
vinothp Avatar asked Dec 27 '22 19:12

vinothp


1 Answers

Finally Here's how I did it.

In didFinishLaunchingWithOptions:

//save the root view controller
[[self window] makeKeyAndVisible];
UINavigationController *navigationController = (UINavigationController*) self.window.rootViewController;
rootController = [[navigationController viewControllers] objectAtIndex:0];

Somewhere else in the app delegate:

[rootController performSegueWithIdentifier:@"destinationSegue" sender:self];

Then, in the storyboard, create a segue from the view that gets assigned to "rootController" to the desired optional view, and give that new segue the id destinationSegue. It takes some debugging to make sure the rootController variable gets assigned to the correct view.

like image 147
vinothp Avatar answered Feb 22 '23 22:02

vinothp