How to use the device token in method other than didRegisterForRemoteNotificationsWithDeviceToken?

后端 未结 2 520
说谎
说谎 2020-12-21 15:11

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

相关标签:
2条回答
  • 2020-12-21 15:47

    In your app delegate class define method + (CustomAppDelegate *)sharedAppDelegate whose implementation should look like this one:

    + (CustomAppDelegate *)sharedAppDelegate
    {
         return (CustomAppDelegate *) [UIApplication sharedApplication].delegate;
    }
    

    where CustomAppDelegate is name of your app delegate class.

    In method your need to get value of str variable you should type following:

    NSString *token = [[CustomAppDelegate sharedAppDelegate] str];
    

    where CustomAppDelegate is name of your app delegate class and str is synthesized property (or name of the method) where device token is stored.

    Before calling sharedAppDelegate don't forget to import "CustomAppDelegate.h"

    0 讨论(0)
  • 2020-12-21 15:48

    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"];
    
    0 讨论(0)
提交回复
热议问题