点击通知时如何处理 Swift 3 中的启动选项?遇到语法问题 [英] How to handle launch options in Swift 3 when a notification is tapped? Getting syntax problems

查看:13
本文介绍了点击通知时如何处理 Swift 3 中的启动选项?遇到语法问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理启动选项并在点击我在 swift 3 中收到的远程通知时打开一个特定的视图控制器.我看到了类似的问题,例如 这里,但对于新的 swift 3 实施没有任何帮助.我在 AppDelegate.swift 中看到了一个类似的问题(和)我在 didFinishLaunchingWithOptions 中有以下内容:

I am trying to handle the launch option and open a specific view controller upon tapping a remote notification that I receive in swift 3. I have seen similar question, for instance here, but nothing for the new swift 3 implementation. I saw a similar question (and ) In AppDelegate.swift I have the following in didFinishLaunchingWithOptions:

    var localNotif = (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as! String)
if localNotif {
    var itemName = (localNotif.userInfo!["aps"] as! String)
    print("Custom: (itemName)")
}
else {
    print("//////////////////////////")
}

但是 Xcode 给了我这个错误:

but Xcode is giving me this error:

Type '[NSObject: AnyObject]?' has no subscript members

我也试过这个:

   if let launchOptions = launchOptions {
        var notificationPayload: NSDictionary = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as NSDictionary!

    }

我得到这个错误:

error: ambiguous reference to member 'subscript'

我以前使用类似代码通过键从字典中获取值的任何地方都遇到了类似的错误,我必须替换代码并首先安全地解开字典.但这似乎在这里不起作用.任何帮助,将不胜感激.谢谢.

I got similar errors wherever I had previously used similar code to get a value from a dictionary by the key and I had to replace the codes and basically safely unwrap the dictionary first. But that doesn't seem to work here. Any help would be appreciated. Thanks.

推荐答案

结果发现整个方法签名已经改变,当我实现新的签名时一切正常.下面是代码.

So it turned out the whole method signature has changed and when I implemented the new signature things worked just fine. Below is the code.

新的 didFinishLaunchingWithOptions 方法:

new didFinishLaunchingWithOptions method:

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



//and then 
 if launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] != nil {


// Do what you want to happen when a remote notification is tapped.


}

}

希望这会有所帮助.

这篇关于点击通知时如何处理 Swift 3 中的启动选项?遇到语法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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