Swift 3中如何处理启动选项以响应通知?遇到语法问题。

20

我正在尝试处理启动选项,并在接收到远程通知后点击打开特定的视图控制器(Swift 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'

我之前在类似的代码中使用字典键获取值时,遇到了类似的错误,不得不替换代码并安全地解包字典。但在这里似乎行不通。希望能得到帮助。谢谢。

5个回答

34

苹果在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的内容请阅读苹果公司的文档


我仍然收到错误信息,成员下标的引用不明确! - TheBen
我的错,有一个错误。现在请再试一次。 - Adeel Miraj
嗯,谢谢,但你确定更新了你的回复吗?看起来还是一样的。 - TheBen
1
是的,我做了一些更改。它们非常微小,所以不太明显。 - Adeel Miraj
正如我在上面的答案中提到的,didfinishwithoptions方法的整个签名已经改变。再次感谢您的评论。 - TheBen
显示剩余7条评论

16

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

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


}

}
希望这可以帮助到你。

7

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

1
如何在 didReceiveRemoteNotification 函数中传递此通知对象? - Vinoth Vino
抱歉,我不太明白你的意思,请详细说明一下,这样我才能帮助你。 - Amr Angry

0
if let remoteNotif = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: Any] {
       if let notification = remoteNotif["aps"] as? [AnyHashable : Any] {
               //handle PN
       }
}

0

Swift 3:

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

            #if DEBUG
                print("iOS9 didFinishLaunchingWithOptions notification\n \(notification)")
            #endif

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接