gpt4 book ai didi

ios - swift 代码 : how to launch a specific view controller after receiving a remote push notification

转载 作者:可可西里 更新时间:2023-11-01 06:06:07 24 4
gpt4 key购买 nike

在 Swift 中,我的 ma​​in.storyboard 有 2 个 View Controller (mainVCsecondVC),它们都嵌入在它们的导航 Controller 中。收到远程推送通知时,我如何更改 AppDelegate 中的代码以使 secondVC View Controller 每次都启动。我目前有代码在 didReceiveRemoteNotification 中发布通知,并且在 secondVC 中有代码来添加观察者

AppDelegate.swift中,我的代码如下:

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)

}

secondVC中,我有如下代码:

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)
}

最佳答案

这应该有效。您的 UINavigationController 的 viewControllers 属性是嵌入在导航 Controller 中的所有 View Controller 的数组。所以如果你想要你的第二个 vc,你可以像典型的数组一样访问它,所以 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)
}
}

关于ios - swift 代码 : how to launch a specific view controller after receiving a remote push notification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29932734/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com