为什么appdelegate中的函数没有被调用?

sta*_*eet 2 ios parse-platform swift

我试图将当前用户添加到安装类,但由于某种原因,该函数未被调用.我究竟做错了什么?我该如何解决?下面是finishlaunchingwithoptions和didregisterfornotifications.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.



    Parse.enableLocalDatastore()

    // Initialize Parse.
    Parse.setApplicationId("***********************",
        clientKey: "****************************")




    let settings = UIUserNotificationSettings(forTypes: [.Alert, .Sound, .Badge], categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
    UIApplication.sharedApplication().registerForRemoteNotifications()

    if let launchOptions = launchOptions as? [String : AnyObject] {
        if let notificationDictionary = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject : AnyObject] {
            self.application(application, didReceiveRemoteNotification: notificationDictionary)
        }
    }

    return true
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    // Store the deviceToken in the current Installation and save it to Parse
    let installation = PFInstallation.currentInstallation()
    installation.setDeviceTokenFromData(deviceToken)
    installation["user"] = PFUser.currentUser()
    installation.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
        if (error == nil){
            print("saved installation")
        }else{
            print(error)
        }
    })


}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError!) {
    print("Couldn't register: \(error)")
}
Run Code Online (Sandbox Code Playgroud)

Two*_*aws 5

这种情况的问题是你在iOS模拟器中运行,它不支持远程通知.所以,你didRegisterForRemoteNotificationsWithDeviceToken永远不会被召唤,因为你在模拟器中.试试设备,我怀疑它会第一次工作.

对于发现此问题并遇到类似问题的未来读者,诊断问题的最佳方法是将其添加到您的应用代理:

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    print("Failed to register for remote notifications: \(error.localizedDescription)");
}
Run Code Online (Sandbox Code Playgroud)