简体   繁体   English

如何在Swift 4上的didFinishLaunchingWithOptions中获取Notificationcenter数据

[英]How to get Notificationcenter data from didFinishLaunchingWithOptions on swift 4

I'm working on an app that receives data from a remote notification, I'm trying to pass that data from didFinishLaunchingWithOptions to my ViewController using Notificationcenter when opening the app through tapping on the notification using launchOptions . 我正在开发一个从远程通知中接收数据的应用程序,当通过使用launchOptions轻按Notificationcenter打开应用程序时,我正在尝试使用Notificationcenter将数据从didFinishLaunchingWithOptions传递至ViewController The problem is that the observer on my viewDidAppear is not getting any data. 问题是我的viewDidAppear上的观察者没有获取任何数据。

This is my code on the didFinishLaunchingWithOptions method: 这是我在didFinishLaunchingWithOptions方法上的代码:

func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {


    if let remoteNotification = launchOptions?[.remoteNotification] as?  [AnyHashable : Any] { 

             let nameSchool = remoteNotification["name_school" as! String]
              NotificationCenter.default.post(name: Notification.Name.nameSchool, object: nameSchool)

                }
    }

And the observer in the viewDidAppear method: 观察者在viewDidAppear方法中:

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)


NotificationCenter.default.addObserver(forName: Notification.Name.nameSchool, object: nil, queue: OperationQueue.main) { (nameSchool) in


            let schoolName = nameSchool.object as! String 

            self.messagePopup(message: "Data received")

        }
}

Since your application(,didFinishLaunchingWithOptions:) will be called before the viewDidAppear (as per your comment), then you will have to temporarily store the result you get from the function until your code can retrieve it later. 由于您的应用程序(,didFinishLaunchingWithOptions :)将在viewDidAppear之前(根据您的评论)被调用,因此您将必须临时存储从函数中获得的结果,直到您的代码以后可以检索它为止。

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var remoteNotificationAtLaunch: [AnyHashable: Any]?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        self.remoteNotificationAtLaunch = launchOptions?[.remoteNotification] as?  [AnyHashable : Any]
    }

    ...
}

Obviously keep the part you already have in your AppDelegate that generates the post to NotificationCenter when a remote notification is received. 显然,在收到远程通知时,将您已经拥有的部件保留在AppDelegate中,该部件会在NotificationCenter中生成帖子。 Then in your view controller, update your viewDidAppear... 然后在您的视图控制器中,更新viewDidAppear ...

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    observeNotifications()
    checkForNotificationAtLaunch()
}

private func observeNotifications() {
    NotificationCenter.default.addObserver(forName: Notification.Name.nameSchool, object: nil, queue: OperationQueue.main) { (nameSchool) in
        let schoolName = nameSchool.object as! String
        self.processNotification(schoolName: schoolName)
    }
}

private func checkForNotificationAtLaunch() {
    if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
        if let notificationAtLaunch = appDelegate.remoteNotificationAtLaunch,
            let schoolName = notificationAtLaunch["name_school"] as? String {
            processNotification(schoolName: schoolName)
        }
    }
}

private func processNotification(schoolName: String) {
    self.messagePopup(message: "data received")
    // do something with schoolName....
}

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

相关问题 NotificationCenter 在 Swift 传递数据 - NotificationCenter to pass data in Swift 如何使用swift 3.0中的NotificationCenter和swift 2.0中的NSNotificationCenter传递数据? - How to pass data using NotificationCenter in swift 3.0 and NSNotificationCenter in swift 2.0? 如何在Swift4中修复NotificationCenter - How to fix NotificationCenter in swift4 无法从didFinishLaunchingWithOptions函数获取launchOptions数据 - Can't get launchOptions data from didFinishLaunchingWithOptions function 如何在didFinishLaunchingWithOptions中获取设备令牌 - How to Get Device Token in didFinishLaunchingWithOptions NotificationCenter 无法从 Swift iOS 中的 AppDelegate 工作 - NotificationCenter is not working from AppDelegate in Swift iOS 如何在iOS Swift中使用不同类的选择器功能,主要用于NotificationCenter - How to use selector function from different class in iOS Swift, mainly for NotificationCenter 使用 NotificationCenter 将数据从 CollectionView 发送到 CollectionView 2 - Sending data from CollectionView to CollectionView two with NotificationCenter 观察者不会使用NotificationCenter Swift 4.0打电话 - Observers don't get called using NotificationCenter Swift 4.0 Swift 3中的NotificationCenter崩溃 - NotificationCenter Crash in Swift 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM