如何从 AppDelegate 打印 o NSLog?
How to print o NSLog from AppDelegate?
我只是想看看我的令牌设备。
我就是这样做的:
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
}
但是print(cleanToken)
什么也没做。不知道是不是不能在AppDelegate中使用print
。我还创建了一个全局变量并将 cleanToken
保存到该新变量中。然后我在 viewController 中打印它,但它是空的。
您可能 运行在模拟器中进行此操作吗?那么您可能不会在 didRegisterForRemoteNotificationsWithDeviceToken
接到电话。您需要 运行 在实际 phone
已修复。我刚刚收到有关我的应用程序已停用的通知,提示代码无效。我已经卸载了该应用程序并重新安装它,所以它可以完美运行。
我只是想看看我的令牌设备。
我就是这样做的:
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
}
但是print(cleanToken)
什么也没做。不知道是不是不能在AppDelegate中使用print
。我还创建了一个全局变量并将 cleanToken
保存到该新变量中。然后我在 viewController 中打印它,但它是空的。
您可能 运行在模拟器中进行此操作吗?那么您可能不会在 didRegisterForRemoteNotificationsWithDeviceToken
接到电话。您需要 运行 在实际 phone
已修复。我刚刚收到有关我的应用程序已停用的通知,提示代码无效。我已经卸载了该应用程序并重新安装它,所以它可以完美运行。