ios - Swift code: how to launch a specific view controller after receiving a remote push notification -
in swift, main.storyboard had 2 view controllers (mainvc, secondvc), both embedded in navigation controller. when receiving remote push notification, how change code in appdelegate have secondvc view controller launch everytime. have code in post notification in didreceiveremotenotification, , have code in secondvc add observer
in appdelegate.swift, code follows:
func application(application: uiapplication, didfinishlaunchingwithoptions launchoptions: [nsobject: anyobject]?) -> bool { var type = uiusernotificationtype.badge | uiusernotificationtype.alert | uiusernotificationtype.sound var setting = uiusernotificationsettings(fortypes: type, categories: nil) uiapplication.sharedapplication().registerusernotificationsettings(setting) uiapplication.sharedapplication().registerforremotenotifications() return true } func application(application: uiapplication, didreceiveremotenotification userinfo: [nsobject : anyobject]) { nsnotificationcenter.defaultcenter().postnotificationname("mynotificationtype", object: nil, userinfo: userinfo) } in secondvc, have code follows:
override func viewwillappear(animated: bool) { println("viewwillappear()") nsnotificationcenter.defaultcenter().addobserver(self, selector: "authenticatewithtouchid:", name: "mynotificationtype", object: nil) } override func viewwilldisappear(animated: bool) { println("viewwilldisapper") nsnotificationcenter.defaultcenter().removeobserver(self, name: "mynotificationtype", object: nil) }
this should work. uinavigationcontroller's viewcontrollers property array of view controllers embedded in navigation controller. if want second vc, access typical array, viewcontrollers[1]
func application(application: uiapplication, didreceiveremotenotification userinfo: [nsobject : anyobject]) { var navcontroller:uinavigationcontroller = self.window?.rootviewcontroller as! uinavigationcontroller if navcontroller.viewcontrollers != nil { var vc:customviewcontroller = navcontroller.viewcontrollers[1] as! customviewcontroller navcontroller.presentviewcontroller(vc, animated: false, completion: nil) } }
Comments
Post a Comment