I am trying to find a way to make it possible for the user to find their device token (for debugging reasons).
I have tried this:
AppDelegate.h
...
@interface MyAppDelegate : UIResponder <UIApplicationDelegate> {
NSString *token;
}
@property (nonatomic, retain) NSString *token;
...
AppDelegate.m
...
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
_token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
_token = [_token stringByReplacingOccurrencesOfString:@" " withString:@""];
NSUInteger lenthtotes = [_token length];
NSUInteger req = 64;
if (lenthtotes == req){
NSLog(@"uploaded token: %@", _token);
upload_token = _token;
} else {
_token = @"";
}
...
ViewController.m
NSString *token;
- (void)viewDidLoad
{
[super viewDidLoad];
MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];
token = appDelegate.token;
NSLog(token);
...
The log within the AppDelegate
works fine and returns the device token.
But the log inside the ViewController
returns nothing how come ?
It is normal that you can't retrieve the token this way because in the
viewDidLoad
method of your view controller you didn't get your token yet. Instead of retrieving it from theAppDelegate
, you should push it. I would use a notification for that :AppDelegate.m
ViewController.m