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

In my application, I use the alarm function. It works great. When I press the right button, it launches my application. But I want to run View Controller, which is not rootViewController. I tried searching on Google and SO, but I could not understand or imagine.

I am looking for any example to achieve this.

Thanks for helping the guys.

EDIT

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Add the view controller 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]; } 
+4
source share
4 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 application delegate:

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

Then, in the storyboard, create a segue from the view that is assigned the "rootController" to the desired optional view and give this new segment the destinationSegue identifier. Some debugging is required to ensure that the rootController variable is set to the correct view.

+6
source

You do not need to create a segue. You only need to assign an identifier in the storyboard for the ViewController.

  [[self window] makeKeyAndVisible]; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; LensOverviewViewController *editLensViewController = [storyboard instantiateViewControllerWithIdentifier:@"lensOverView"]; UINavigationController *yourViewController = [[UINavigationController alloc] initWithRootViewController:editLensViewController]; [self.window.rootViewController presentViewController:yourViewController animated:NO completion:nil]; 

Usually [[self window] makeKeyAndVisible]; is not in the didFinishLaunchingWithOptions function, but it must be called explicity to make the rootviewcontroller visible.

+3
source
 UIWindow *window = [[UIApplication sharedApplication] keyWindow]; UIViewcontroller *rootViewController = [window rootViewController]; [rootViewController presentModalViewController:alarmViewController animated:YES]; 
+1
source

When the application starts, look for the following method in the launchOptionsKey UIApplicationLaunchOptionsLocalNotificationKey declaration:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

This will let you know if the application is running due to a local notification (the one you use for alarm).

You can also do something similar:

 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 

Once you determine that you have really received a notification, simply find the root view controller from the window and present the view controller that you want to present in the text.

0
source