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

2023-07-29移动开发问题
4

本文介绍了点击通知时如何处理 Swift 3 中的启动选项?遇到语法问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试处理启动选项并在点击我在 swift 3 中收到的远程通知时打开一个特定的视图控制器.我看到了类似的问题,例如 这里,但对于新的 swift 3 实施没有任何帮助.我在 AppDelegate.swift 中看到了一个类似的问题(和)我在 didFinishLaunchingWithOptions 中有以下内容:

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

但是 Xcode 给了我这个错误:

but Xcode is giving me this error:

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

我也试过这个:

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

    }

我得到这个错误:

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.

新的 didFinishLaunchingWithOptions 方法:

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.


}

}

希望这会有所帮助.

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

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

[ios.cocos2d+box2d]如何禁用自动旋转?
[ios.cocos2d+box2d]how to disable auto-rotation?([ios.cocos2d+box2d]如何禁用自动旋转?)...
2024-08-12 移动开发问题
7

从 Documents 目录存储和读取文件 iOS 5
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)...
2024-08-12 移动开发问题
9

如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?
How can I implement a virtual joystick for a cocos2d game outside the cocos2d environment?(如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?)...
2024-08-12 移动开发问题
13