如何在 swift 4 上从 didFinishLaunchingWithOptions 获取通知中心数据

Posted

技术标签:

【中文标题】如何在 swift 4 上从 didFinishLaunchingWithOptions 获取通知中心数据【英文标题】:How to get Notificationcenter data from didFinishLaunchingWithOptions on swift 4 【发布时间】:2018-08-15 05:16:04 【问题描述】:

我正在开发一个从远程通知接收数据的应用程序,我正在尝试使用Notificationcenter 将数据从didFinishLaunchingWithOptions 传递给我的ViewController,当通过点击使用@ 的通知打开应用程序时987654324@。问题是我的viewDidAppear 上的观察者没有得到任何数据。

这是我在 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)

                
    

以及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")

        

【问题讨论】:

如果您在视图控制器的 viewDidAppear 和应用程序委托的 didFinishLaunching 中设置断点,您能否确认您的 viewDidAppear 确实首先击中了断点? didFinishLaunching 首先命中断点。我在 didFinishLaunching 的末尾设置了一个,在 viewDidAppear 的开头设置了另一个。首先是 didFinishLaunching。 【参考方案1】:

由于您的 application(,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]
    

    ...

当收到远程通知时,显然保留 AppDelegate 中已有的部分,该部分会生成到 NotificationCenter 的帖子。然后在您的视图控制器中,更新您的 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....

【讨论】:

就是这样!我终于让它工作了。感谢您,我能够解决我的应用程序上的通知问题。非常感谢你:)

以上是关于如何在 swift 4 上从 didFinishLaunchingWithOptions 获取通知中心数据的主要内容,如果未能解决你的问题,请参考以下文章

如何在 swift4 上从服务器播放 mp3 声音?

如何在 Swift 上从最后一个 VC 更改为中间 VC

在 Swift 上从 URL 加载/下载图像

在 Swift 上从 URL 加载/下载图像

为啥我在 iOS 上从 Swift 调用服务时收到 SSL 错误?

我可以在Swift上从Web服务器获取3D模型吗?