iOS Swift从推送通知以编程方式导航到某些ViewController [英] iOS Swift Navigate to certain ViewController programmatically from push notification

查看:178
本文介绍了iOS Swift从推送通知以编程方式导航到某些ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用推送通知来通知用户应用中发生的不同类型的事件。某种类型的推送通知应该打开一个特殊的视图控制器(关于新的聊天消息的通知确实导航到点击聊天)

I use push notifications to inform the user about different types of events happening in the app. A certain type of push notification should open a special viewcontroller (f.e a notification about a new chat message does navigate to the chat on click)

为了实现这一点,我尝试了我的app委托中的代码如下:

To achieve this, i tried the following code inside my app delegate:

func applicationDidBecomeActive(_ application: UIApplication) {
 if var topController = UIApplication.shared.keyWindow?.rootViewController {
        while let presentedViewController = topController.presentedViewController {
            topController = presentedViewController
        }
        topController.tabBarController?.selectedIndex = 3
    }
}

它不会移动到任何地方。我在这里缺少什么?

It does not move anywhere. What am I missing here?

推荐答案

我写了一个简单的类,可以从一个地方导航到视图层次结构中的任何视图控制器通过传递类类型的代码行,所以您编写的代码也将与视图层次结构本身分离,例如:

I wrote a simple class to navigate to any view controller in the view hierarchy from anywhere in one line of code by just passing the class type, so the code you'll write will be also decoupled from the view hierarchy itself, for instance:

Navigator.find(MyViewController.self)?.doSomethingSync()
Navigator.navigate(to: MyViewController.self)?.doSomethingSync()

..或者您也可以在主线程上异步执行方法:

..or you can execute methods asynchronously on the main thread also:

Navigator.navigate(to: MyViewController.self) { (MyViewControllerContainer, MyViewControllerInstance) in
    MyViewControllerInstance?.doSomethingAsync()
}

这是GitHub项目链接: https:// github。 com / oblq / Navigator

Here's the GitHub project link: https://github.com/oblq/Navigator

这篇关于iOS Swift从推送通知以编程方式导航到某些ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆