无法从didFinishLaunchingWithOptions函数获取launchOptions数据

时间:2015-05-18 16:08:44

标签: ios swift apple-push-notifications

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

  

无法调用' objectForKey'使用类型'(字符串)'。

的参数列表
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
}

1 个答案:

答案 0 :(得分:2)

尝试以下方法:

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

    // extra work           
}