Philip
Philip

Reputation: 2285

iOS Show specific viewcontroller via local notification

I have implemented local notifications in my app but I want to choose which viewcontroller to show when the user is "swiping" the notification. My app is a few viewcontrollers with basic segue navigation between them.

How do choose which viewcontroller to view?

Upvotes: 2

Views: 2630

Answers (3)

Deepak Bharati
Deepak Bharati

Reputation: 280

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    [[UIApplication sharedApplication] cancelLocalNotification:notification];


    //My_specificViewController
    RingingViewController *ringingVC = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"RingingViewController"];
    [self.window setRootViewController:ringingVC];
}

Upvotes: 3

Duncan C
Duncan C

Reputation: 131408

Greg gave you the bulk of the answer, but note that in addition to didFinishLaunchingWithOptions, you also need to add a application:didReceiveLocalNotification: method. That method gets called once your app is running. I would suggest pulling out the code to handle local notifications into a single method and calling it from both places.

Upvotes: 0

Greg
Greg

Reputation: 25459

You can handle local notification in application:didFinishLaunchingWithOptions method and after that you can choose right view controller:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Handle notification
    UILocalNotification *localNotif =
    [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    UIViewController *vc = nil
    if (localNotif) {
        //base on notification create right view controller
        vc = [[VC alloc] init];
        NSLog(@"Recieved Notification %@",localNotif);
    }
    else
    {
       //create default view controller
        vc = [[VC alloc] init];
    }

    // Add the view controller's view to the window and display.
    _window.rootViewController = vc;
    [_window makeKeyAndVisible];

    return YES;
}

Upvotes: 0

Related Questions