如何从AppDelegate打印o NSLog?

问题描述 投票:0回答:3

我只想查看我的令牌设备。

这就是我所做的:

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 中打印它,但它是空的。

swift token appdelegate
3个回答
1
投票

您可能在模拟器中运行它吗?那么您可能不会接到

didRegisterForRemoteNotificationsWithDeviceToken
的电话。您需要在实际手机上运行它


0
投票

已修复。我刚刚收到我的应用程序已停用的通知,因此代码不起作用。我已卸载该应用程序并再次安装,因此它可以完美运行。


0
投票

在应用程序委托中创建操作系统记录器对象有点奇怪。您需要在每个函数的函数体中创建实际的记录器对象。当尝试在

os_log_create
行下方声明
@implementation AppDelegate
时,我会遇到很多错误。

#import "AppDelegate.h"
#import <os/log.h>

os_log_t logger = nil;

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // other configuration code above
  // I chose to give it the category of "AppDelegate"
  logger = os_log_create("com.bundle.id", "AppDelegate"); 

  self.moduleName = @"main";
  // You can add your custom initial props in the dictionary below.
  // They will be passed down to the ViewController used by React Native.
  self.initialProps = @{};
  os_log(logger, "This is gonna log the message!");
  os_log(logger, "In the logs explorer, you can now Filter by category = AppDelegate");
  
  bool didFinish = [super application:application didFinishLaunchingWithOptions:launchOptions];
   
   return didFinish;
}

@end
© www.soinside.com 2019 - 2024. All rights reserved.