Richard
Richard

Reputation: 1718

How to load ViewController after passing notification

When my application closes I set a UILocalNotification depending on Core-Data and a link to the NSManagedObject is saved in the userInfo property. This works fine.

Then when the UILocalNotification fires, and the user swipes it, the app loads and I use NSNotificationCenter to successfully pass the userInfo to my MainViewController. This works fine.

Here I use managedObjectIDForURIRepresentation to find the Core-Data object and run performSegue to display the DetailViewController. This works fine IF the user closed the application while on the MainViewController

My problem however is depending on where the user is in the application when they exit.

Say for example they exit while viewing the SettingsViewController, then when they access the UILocalNotification it will slide the DetailViewController on top of the SettingsViewController. It is nice that they see the correct Object however I would prefer that the back button took them back to MainViewController

The same goes for if they are in a UIViewController 3 or 4 layers deep into the DetailViewController and exit. When they access the UILocalNotification it slides the DetaiViewController on top of where they are. Again, I would prefer the application to collapse the previous DetailViewController and start a fresh.

Hope this makes sense. Not sure what to do other than try setting up lots of "unwind" segues that are triggered if a UILocalNotification is accessed. Is there a simpler and better approach?

Update

Here is what i'm up to in didReceiveLocalNotification

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    var state:UIApplicationState = application.applicationState
    if state == UIApplicationState.Inactive {
        println("app del notification : \(notification.userInfo)")
        NSNotificationCenter.defaultCenter().postNotificationName(kShowNotification, object: notification.userInfo)
    }
}

Update

Ok, I like the idea of popping the 2 UINavigationControllers (Master & Detail) connected to the UISplitViewController but not sure how to actually do this.

I've setup my subclassed UISplitViewController to receive an NSNotificationCenter message. However this count is always 1:

// MARK: Notifications
func showEventNotication(notification: NSNotification) {
    println("split view controllers: \(self.viewControllers.count)")
}

Here is the current setup:

enter image description here

Upvotes: 1

Views: 1009

Answers (3)

Włodzimierz Woźniak
Włodzimierz Woźniak

Reputation: 3225

It works for me (Swift 2.0). I have TabBarController and NavigationController + ViewControllers. When app receive the localNotification and after tap to button it will show MyViewController. In AppDelegate:

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {

    if identifier == "showDetails" {

        let tabBarController = window?.rootViewController as! UITabBarController
        tabBarController.selectedIndex = 0

        let myViewController = window?.rootViewController!.storyboard?.instantiateViewControllerWithIdentifier("MyViewController") as? MyViewController

        let controller = tabBarController.selectedViewController as! UINavigationController
        controller.pushViewController(myViewController!, animated: false)
    }

    completionHandler()
}

Upvotes: 1

Mehul Patel
Mehul Patel

Reputation: 23053

I haven't done this before. I have a suggestion for you.

It's not good or not appropriate to inject view controller in current navigation hierarchy when notification finish. You can do a brach thing like if you know the git repository that maintains a separate brach for each clone/check out.

So you need to do just like this way. You are in a middle of something and notification shows up then leave current flow of navigation and start a new branch from that point.

So after all digging for this notification related flow at last you have node/junction to safely recover your previous flow of application (Like you said back & back will lead you to previous view controller before notification was received.)

The one way I think to doing of this is presentViewController from current view controller. I haven't try this but it will work.

Get current view controller in didReceiveLocalNotification method and present view controller which is link to notification here.

Pictorial Flow:

NavigationController ----> VC1 ---->  SettingsViewController
                                      /                  |
                                     /                   |
                                    /                    |
                                   /                     |   
                             Back /_______________   Notification received
                                 |                           |
                                 |                           | present view controller
                            Back |                           |   
                                 |                           V
                                 |                  NotificationViewController
                                 |                           |
                                 |                           V
                              Storage <----------------- Operations

Upvotes: 0

Richard
Richard

Reputation: 3297

If you have a reference to the UINavigationController in your AppDelegate you can use the method - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification to change the navigation stack.

If you have the reference to the UINavigationControllersimply call [navController popToRootViewControllerAnimated:NO] to make it pop the stack, then push the MainViewController and then your DetailViewControllerjust like you do now.

Upvotes: 0

Related Questions