I am trying to get the device token in my AppDelegate, and then use it in a function in my ViewController later.
I successfully retrieve the device token like so in the AppDelegate:
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
NSString *tokenAsString = [[[deviceToken description]
stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]
stringByReplacingOccurrencesOfString:@" " withString:@""];
[[NSUserDefaults standardUserDefaults] setObject: token forKey:@"deviceToken"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
Then I am trying to use it in a function in my ViewController, but it is printing as null
:
-(void)addDeviceToken{
NSString *deviceToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"deviceToken"];
NSLog(@"%@", deviceToken);
}
Does anyone know how to get the variable to show up here?