Jay
Jay

Reputation: 2661

How do I find the visibleViewController in appdelegate?

I currently instantiate the storyboards programmatically like so:

var iphone35StoryBoard:UIStoryboard = UIStoryboard(name: "iphone35", bundle: nil)

var initialViewController:UIViewController = iphone35StoryBoard.instantiateInitialViewController() as! UIViewController

self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

self.window?.rootViewController = initialViewController

self.window?.makeKeyAndVisible()

This is my first time working with push notifications, so I'm unsure as the best practice, but what i'm trying to do is in didReceiveRemoteNotification and while the app is in an active state(foreground), I would like to be able to determine what view the user is currently on to determine if i should A) Call a local notificaiton or B) update the view if the push notification corresponds to the visible view.

Using various other SO related questions, I have tried to access the visibleviewcontroller using these variations of code:

let navigationController = application.windows[0].rootViewController as! UINavigationController

1)

let visibleController: AnyObject? = navigationController.visibleViewController

if visibleController is ProfileViewController {
    println("this works")
}

and

2)

let viewControllers: [UIViewController] = navigationController.viewControllers as! [UIViewController]

for vc in viewControllers {
    if vc is ProfileViewController {
        println("this works")
    }
}

3)

if let wd = self.window {
   var vc = wd.rootViewController
   if(vc is UINavigationController){
        vc = (vc as UINavigationController).visibleViewController
    }

    if(vc is ProfileViewController){
        println("this works")
    }
}

the problem i'm running into is that I can only obtain the rootviewcontroller and never the visible view controller. The rootviewcontroller in this case is a login screen, and despite me being on the profileViewController, i can only obtain the loginviewcontroller. Is this the best method for dealing with push notifications?

Upvotes: 1

Views: 750

Answers (1)

Ashish Kakkad
Ashish Kakkad

Reputation: 23882

Move to destination view controller from storyboard when push notification will came

    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
    var destViewController : destVC
    destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("destVC") as! destVC
    var navigationController =  UIApplication.sharedApplication().keyWindow!.rootViewController as! UINavigationController
    navigationController.pushViewController(destViewController, animated: true)

This is working for me from appdelegate.

Use notification center for more stuff :

NSNotificationCenter.defaultCenter().postNotificationName("name", object: nil, userInfo:nil)

NSNotificationCenter.defaultCenter().addObserver(self, selector: "method:", name: "name", object: nil)

func method(notification: NSNotification) {
    //Do your stuff.
}

Passing data with notification center

NSNotificationCenter.defaultCenter().postNotificationName("name", object:nil, userInfo:["message":"Unable to add \(homeName) Home"]) 

func method(notification: NSNotification) {
    //Do your stuff.
    println(notification.userInfo)
}

Upvotes: 1

Related Questions