Search code examples
swifttokenappdelegate

How to print o NSLog from AppDelegate?


I just want to see my token device.

This is what I do:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    let pushSettings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes:[UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound],categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(pushSettings)
    UIApplication.sharedApplication().registerForRemoteNotifications()

    return true
}

// Successfully registered for push
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let trimEnds = deviceToken.description.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "<>"))
    let cleanToken = trimEnds.stringByReplacingOccurrencesOfString(" ", withString: "", options: []); print(cleanToken)

    //registerTokenOnServer(cleanToken) //theoretical method! Needs your own implementation
}

// Failed to register for Push
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    NSLog("Failed to get token; error: %@", error) //Log an error for debugging purposes, user doesn't need to know
}

But print(cleanToken) does nothing. I don't know if I can't use print in AppDelegate. I've also created a global variable and saved the cleanToken to that new variable. Then I print it in my viewController but it's empty.


Solution

  • Fixed. I just had notifications for my app deactivated sos that code didn't work. I've uninstalled the app and installed it again so it works perfectly.