当通知被点击时,如何在Swift 3中处理启动选项?获取语​​法问题 [英] How to handle launch options in Swift 3 when a notification is tapped? Getting syntax problems

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

问题描述

我试图处理启动选项,并打开一个特定的视图控制器,我点击一个远程通知,我收到了迅速3.我见过类似的问题,例如这里,但没有为新的swift 3实现。我看到一个类似的问题(和)在AppDelegate.swift我有以下didFinishLaunchingWithOptions:

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

但是Xcode给了我这个错误:

< pre $ 类型'[NSObject:AnyObject]?'没有下标成员

我也试过:

 如果让launchOptions = launchOptions {
var notificationPayload:NSDictionary = launchOptions [UIApplicationLaunchOptionsRemoteNotificationKey ]作为NSDictionary!


$ / code $ / pre

我得到这个错误:

 错误:模糊引用成员'下标'

我得到了类似的错误,无论我以前使用过类似的代码,通过密钥从字典中获取值,我必须先替换代码,然后基本上安全地解开字典。但是,这似乎并没有在这里工作。任何帮助,将不胜感激。谢谢。所以原来整个方法的签名已经改变了,当我实现了新的签名的时候,工作得很好。下面是代码。

新的didFinishLaunchingWithOptions方法:

$ p $ func应用程序(_ application:UIApplication ,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey:Any]?= nil) - > Bool {



//然后
如果launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification]!= nil {


//当点击一个远程通知时,做你想做的事情。

$ b}

}

希望这有助于。

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

but Xcode is giving me this error:

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.

解决方案

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:

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.

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

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