Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
835 views
in Technique[技术] by (71.8m points)

ios - How to use the device token in method other than 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,

In didRegisterForRemoteNotificationsWithDeviceToken method:

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

In didReceiveRemoteNotification method:

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...