点击通知时如何处理 Swift 3 中的启动选项?遇到语法问题

How to handle launch options in Swift 3 when a notification is tapped? Getting syntax problems

我正在尝试处理启动选项并在点击我在 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("//////////////////////////")
}

但是 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'

无论我以前使用类似代码通过键从字典中获取值,我都会遇到类似的错误,我不得不替换代码并基本上首先安全地打开字典。但这在这里似乎行不通。任何帮助,将不胜感激。谢谢。

Apple 在 Swift 3 和其中之一中进行了大量更改。

编辑:这也适用于 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:

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)
}

有关 LaunchOptionsKey 的更多信息,请阅读 Apple 的 documentation

原来整个方法签名都变了,当我实现新的签名时一切正常。下面是代码。

新的 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:

        if let notification = launchOptions?[.localNotification] as? NSDictionary{

            #if DEBUG
                print("iOS9 didFinishLaunchingWithOptions notification\n \(notification)")
            #endif
if let remoteNotif = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: Any] {
       if let notification = remoteNotif["aps"] as? [AnyHashable : Any] {
               //handle PN
       }
}

Swift 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)")
    }