ios 轻按通知时如何处理 Swift 3 中的启动选项?获取语法问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40209234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle launch options in Swift 3 when a notification is tapped? Getting syntax problems
提问by TheeBen
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:
我正在尝试处理启动选项并在点击我在 swift 3 中收到的远程通知时打开一个特定的视图控制器。我见过类似的问题,例如here,但对于新的 swift 3 实现没有任何帮助。我看到了一个类似的问题(和)在 AppDelegate.swift 我在 didFinishLaunchingWithOptions 中有以下内容:
var localNotif = (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as! String)
if localNotif {
var itemName = (localNotif.userInfo!["aps"] as! String)
print("Custom: \(itemName)")
}
else {
print("//////////////////////////")
}
but Xcode is giving me this error:
但是 Xcode 给了我这个错误:
Type '[NSObject: AnyObject]?' has no subscript members
I also tried this:
我也试过这个:
if let launchOptions = launchOptions {
var notificationPayload: NSDictionary = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as NSDictionary!
}
and I get this error:
我收到这个错误:
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.
在我之前使用类似代码从字典中通过键获取值的任何地方,我都遇到了类似的错误,我不得不替换代码并首先安全地解开字典。但这在这里似乎行不通。任何帮助,将不胜感激。谢谢。
采纳答案by TheeBen
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.
所以事实证明整个方法签名已经改变,当我实现新的签名时,一切都很好。下面是代码。
new didFinishLaunchingWithOptions method:
新的 didFinishLaunchingWithOptions 方法:
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.
}
}
Hope this helps.
希望这可以帮助。
回答by Adeel
Apple made plenty of changes in Swift 3
and this one of them.
苹果在其中进行了大量更改,Swift 3
其中之一。
Edit: This works for Swift 4 as well.
编辑:这也适用于 Swift 4。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//Launched from push notification
let remoteNotif = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: Any]
if remoteNotif != nil {
let aps = remoteNotif!["aps"] as? [String:AnyObject]
NSLog("\n Custom: \(String(describing: aps))")
}
else {
NSLog("//////////////////////////Normal launch")
}
}
Swift 5:
斯威夫特 5:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//Launched from push notification
guard let options = launchOptions,
let remoteNotif = options[UIApplication.LaunchOptionsKey.remoteNotification] as? [String: Any]
else {
return
}
let aps = remoteNotif["aps"] as? [String: Any]
NSLog("\n Custom: \(String(describing: aps))")
handleRemoteNotification(remoteNotif)
}
And for more on LaunchOptionKeys
read Apple's documentation.
有关更多信息,请LaunchOptionKeys
阅读 Apple 的文档。
回答by Amr Angry
Swift 4
斯威夫特 4
// Check if launched from the remote notification and application is close
if let remoteNotification = launchOptions?[.remoteNotification] as? [AnyHashable : Any] {
// Do what you want to happen when a remote notification is tapped.
let aps = remoteNotification["aps" as String] as? [String:AnyObject]
let apsString = String(describing: aps)
debugPrint("\n last incoming aps: \(apsString)")
}
回答by ingconti
swift 3:
快速3:
if let notification = launchOptions?[.localNotification] as? NSDictionary{
#if DEBUG
print("iOS9 didFinishLaunchingWithOptions notification\n \(notification)")
#endif
回答by Svitlana
if let remoteNotif = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: Any] {
if let notification = remoteNotif["aps"] as? [AnyHashable : Any] {
//handle PN
}
}