如何使用方法的设备令牌比didRegisterForRemoteNotificationsWithDeviceToken其他? [英] How to use the device token in method other than 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);
当我做这样的设备令牌
正在恢复为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屋!