vinothp
vinothp

Reputation: 10079

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];   
}

Upvotes: 4

Views: 5355

Answers (4)

Ilker Baltaci
Ilker Baltaci

Reputation: 11787

You do not need to create a segue.You only need to assign an id 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];

Normally [[self window] makeKeyAndVisible]; does not reside in the didFinishLaunchingWithOptions function but it needs to be called explicity to make the rootviewcontroller visible.

Upvotes: 3

vinothp
vinothp

Reputation: 10079

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.

Upvotes: 6

J2theC
J2theC

Reputation: 4452

When your application is launched, look for the UIApplicationLaunchOptionsLocalNotificationKey on the launchOptions dictionary on the following method:

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

This will let you know if your application is being launched because of a local notification (the one you use for the alarm).

You can also do something similar on:

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

Once you determine that you actually received a notification, just find the root view controller from the window and present the view controller you want to present modally.

Upvotes: 0

gsach
gsach

Reputation: 5775

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

Upvotes: 1

Related Questions