如何在 didRegisterForRemoteNotificationsWithDeviceToken 以外的方法中使用设备令牌? [英] How to use the device token in method other than didRegisterForRemoteNotificationsWithDeviceToken?

查看:14
本文介绍了如何在 didRegisterForRemoteNotificationsWithDeviceToken 以外的方法中使用设备令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 didRegisterForRemoteNotificationsWithDeviceToken 方法获得了设备令牌.我想以另一种方法使用设备令牌.我是这样试的,

I got the device token through didRegisterForRemoteNotificationsWithDeviceToken method. I wanted to use the device token in another method. I tried it in this way,

didRegisterForRemoteNotificationsWithDeviceToken 方法中:

str = [NSString stringWithFormat:@"%@",deviceToken];
// str is the NSString which is declared in the appDelegate.h file as global variable

didReceiveRemoteNotification方法中:

 NSLog(@"Device Token : %@",str);  

当我这样做时,Device Token 返回为nosniff".

When i do like this the Device Token is returning as "nosniff".

如何将此设备令牌存储在全局变量中并在其他类或其他方法中使用它.

How can i store this device token in a global variable and use it in the other class or in other method.

推荐答案

您可以像这样将设备令牌添加到 NSUserDefaults 字典:

You can add the device token to the NSUserDefaults dictionary like so:

-(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    [[NSUserDefaults standardUserDefaults] setObject:deviceToken forKey:@"deviceToken"];

然后可以通过其他方法访问它,如下所示:

This can then be accessed in other methods like so:

NSString *deviceToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"deviceToken"];

这篇关于如何在 didRegisterForRemoteNotificationsWithDeviceToken 以外的方法中使用设备令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆