当通知被点击时,如何在Swift 3中处理启动选项? 获取语​​法问题

我正在尝试处理启动选项,并打开一个特定的视图控制器,我点击一个远程通知,我收到了迅速3.我已经看到类似的问题,例如在这里 ,但没有为新的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("//////////////////////////") } 

但Xcode给我这个错误:

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

我也试过这个:

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

我得到这个错误:

 error: ambiguous reference to member 'subscript' 

我以前使用过类似的代码,通过密钥从字典中获取值,我得到了类似的错误,我不得不更换代码,基本上安全地解开字典。 但是,这似乎并没有在这里工作。 任何帮助,将不胜感激。 谢谢。

所以原来整个方法签名已经改变了,当我实现新签名的时候,工作得很好。 下面是代码。

new 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. } } 

希望这可以帮助。

苹果Swift 3和其中的一个中做了很多改变。 用这个:

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { //Launched from push notification let remoteNotif = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary if remoteNotif != nil { let aps = remoteNotif!["aps" as NSString] as? [String:AnyObject] NSLog("\n Custom: \(String(describing: aps))") } else { NSLog("//////////////////////////Normal launch") } } 

有关LaunchOptionKeys更多信息, LaunchOptionKeys阅读Apple的文档 。

迅捷3:

  if let notification = launchOptions?[.localNotification] as? NSDictionary{ #if DEBUG print("iOS9 didFinishLaunchingWithOptions notification\n \(notification)") #endif