简体   繁体   English

从AppDelegate通知视图控制器的正确方法是什么?

[英]What is correct way to notify view controller from AppDelegate?

I registered my application to open specific file type (cvs in my case). 我注册了我的应用程序以打开特定的文件类型(在我的情况下为cvs)。 So when user touches "Open in -> My App" 所以当用户触摸“打开 - >我的应用程序”时

application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:])

function is triggered. 功能被触发。 In this function I read data from file to local array. 在这个函数中,我从文件读取数据到本地数组。 In my View Controller I need to display above data. 在我的View Controller中,我需要显示上面的数据。 So what is correct way to notify VC that data were received and pass data to it? 那么通知VC接收数据并将数据传递给它的正确方法是什么?

You need to post a notification like this: 你需要发布这样的通知:

Somewhere in your Constants file: 在Constants文件中的某个位置:

extension Notification.Name {
    public static let myNotificationKey = Notification.Name(rawValue: "myNotificationKey")
}

In AppDelegate: 在AppDelegate中:

let userInfo = [ "text" : "test" ] //optional
NotificationCenter.default.post(name: .myNotificationKey, object: nil, userInfo: userInfo)

In ViewController's viewDidLoad: 在ViewController的viewDidLoad中:

NotificationCenter.default.addObserver(self, selector: #selector(self.notificationReceived(_:)), name: Notification.Name.myNotificationKey, object: nil)

Callback in view controller: 视图控制器中的回调:

func notificationReceived(_ notification: Notification) {
    //getting some data from userInfo is optional
    guard let text = notification.userInfo?["text"] as? String else { return } 
    //your code here
}

The above answer by Alex works, if your view controller that handles the notification happens to be on screen when the notification comes in, but often times its not. Alex的上述答案是有效的,如果处理通知的视图控制器恰好在通知进入时屏幕上,但通常不是。 In

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any])

You want to update your badge count, and then check the notification. 您想更新徽章计数,然后检查通知。 Depending on what kind of notification you have you either handle it silently (perhaps using the Notification Center method above) or you launch the appropriate view controller and either pass it the whole notification or just the id and have the view controller call your API with the id to get all of the details. 根据您拥有的通知类型,您可以静默处理它(可能使用上面的Notification Center方法),也可以启动相应的视图控制器并将整个通知传递给它或只传递ID,并让视图控制器调用您的API id获取所有细节。 You do this just as you would normally change view controllers, so if its a navigation controller, you instantiate the new view controller, pass it the data, then push it on the navigation controller. 您可以像通常更改视图控制器一样执行此操作,因此如果是导航控制器,则实例化新视图控制器,将数据传递给导航控制器,然后将其推送到导航控制器。

    let notificationTableViewController = UIStoryboard(name: Identifiers.Storyboard.Notification, bundle: nil).instantiateViewController(withIdentifier: String(describing: NotificationTableViewController.self)) as!
    NotificationTableViewController
    controller.notificationId = notificationId
    rootNavigationController?.pushViewController(notificationTableViewController, animated: true)

If you have a tab bar application you switch tabs first. 如果您有标签栏应用程序,则首先切换标签。 If you have some kind of custom navigation, you need to call the appropriate method on your container class. 如果您有某种自定义导航,则需要在容器类上调用适当的方法。

Don't need to use Notifications. 不需要使用通知。 Your app will be a mess. 你的应用程序将是一团糟。

JLRoutes can help you! JLRoutes可以帮到你! see the examples in github page. 请参阅github页面中的示例。

Define some URLs just like xxxDetail, xxxList with JLRoutes in your app. 在您的应用中定义一些URL,就像xxxDetail,xxxList和JLRoutes一样。 when application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) get called. application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:])被调用时。 you just need to call [JLRoutes route: URL] 你只需要调用[JLRoutes route: URL]

You can also handle didReceiveRemoteNotification in the same way. 您也可以以相同的方式处理didReceiveRemoteNotification

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
 // get URL from userInfo
 // [JLRoutes route: url];
}

let the server-side send you userInfo with URLs you defined. 让服务器端向您发送带有您定义的URL的userInfo。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM