Open specific viewcontroller when opening app from

2019-03-22 06:52发布

问题:

This question already has an answer here:

  • Open a view controller when a iOS push notification is received 4 answers

As the title implies: how do I open a specific viewcontroller when a user starts the app by swiping on a push notification?

Thanks!

回答1:

Do the following,

in your apps main view controller "viewDidLoad" method add an observer,

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

And also add a method

func SomeNotificationAct(notification: NSNotification){     
    dispatch_async(dispatch_get_main_queue()) {
     self.performSegueWithIdentifier("NotificationView", sender: self)
    }
}

and add the below code in your "didReceiveRemoteNotification" method of Appdelegate class of your app

 func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]){

     NSNotificationCenter.defaultCenter().postNotificationName("SomeNotification", object:nil, userInfo:someData)
}