ios - 无法从 didFinishLaunchingWithOptions 函数获取 launchOptions 数据

标签 ios swift apple-push-notifications

我的应用程序需要从 Remotenotification 获取一些数据,以便将第一页推送到特定页面,例如 FACEBOOK 应用程序,但现在我无法从它始终显示的 didFinishLaunchingWithOptions 函数获取 launchOptions 数据:

Cannot invoke 'objectForKey' with an argument list of type '(String)'.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    let userNotificationTypes = (UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound)

    // Register for Push Notitications, if running iOS 8
    if application.respondsToSelector("registerUserNotificationSettings:") {
        let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: userNotificationTypes, categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()

    } else {
        // Register for Push Notifications before iOS 8
        application.registerForRemoteNotificationTypes( UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound | UIRemoteNotificationType.Alert )
    }

    if let launchOpts = launchOptions {
        var notificationPayload: NSDictionary = launchOptions.objectForKey(UIApplicationLaunchOptionsRemoteNotificationKey) as NSDictionary
        //Cannot invoke 'objectForKey' with an argument list of type '(String)'.
    }

    return true
}

最佳答案

试试下面的方法:

if let notification = launchOpts[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject : AnyObject] {

    // extra work           
}

关于ios - 无法从 didFinishLaunchingWithOptions 函数获取 launchOptions 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30308049/

相关文章:

ios - 未检测到 AppID

ios - 如何在 swift 5 中消除弹跳?

ios - 为什么我的 ios 应用程序不提示我获得推送通知的权限

ios - 如何在 iOS 中加密文件名和文件夹名?

c# - 返回 HTTP 401 并说客户端不要重复请求

ios - 当 UINavigationController 在滚动时隐藏时状态栏变为黑色

swift - JSON结果(NSArray): valueForKey doesn't work as expected

ios - 过去 24 小时 Xcode 中有 1 个版本缺少 dSYM?

iphone - 帮助创建 APNs pem

java - 来自服务器的 APNS 推送通知