如何使用方法的设备令牌比didRegisterForRemoteNotificationsWithDeviceToken其他? [英] How to use the device token in method other than didRegisterForRemoteNotificationsWithDeviceToken?

查看:122
本文介绍了如何使用方法的设备令牌比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屋!

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