Hello, I am able to use the didRegisterForRemoteNotificationWithDeviceToken callback method to get the device token of my iphone when subscribing to push notifications. My question is how can I get this token again a later time? When a user subscribes to something in my application, I want to send the device token and the id of the item they are subscribing to...but I can't figure out where to get the device token from. I tried using the uniqueIdentifer from the UIDevice class but this value is different than what the original token was. I supposed I could call registerForRemoteNotificationTypes each time my app starts to produce the token. But if I do that, I'm not sure how I can access this value from a different class (my didRegisterForRemoteNotificationWithDeviceToken callback is located in the main application delegate). Thanks for any help for an objective C newbie!
+1
A:
I would set a property in your appDelegate that can be accessed from anywhere, and set it to the device token.
// .h
@interface SomeAppDelegate : NSObject <UIApplicationDelegate> {
NSString * dToken;
}
@property (nonatomic, retain) NSString * dToken;
// .m
@implementation SomeAppDelegate;
@synthesize dToken;
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString * token = [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding];
[self setDToken:token];
[token release];
}
- (void)dealloc {
[dToken release]
[super dealloc];
}
You can then access that token anywhere by using:
NSString * token = [(SomeAppDelegate*)[[UIApplication sharedApplication] delegate] dToken];
Tom Irving
2010-05-09 10:11:25
Thank you for the reply. I am able to set the variable in the delegate and retrieve it using [self deviceId] from within the delegate class. However, when I try to retrieve it using the code you provided it does not work. The application compiles, but when it gets to that line I get an "EXC_BAD_ACCESS" error with no other information. I am having a lot of trouble with the objective c syntax and the lack of a reasonable error message is making this really hard to figure out. Any help would be grately appreciated.
Greg
2010-05-09 21:56:32
Nevermind, I got it to work. I'm not sure why but when I did not release the token String it worked fine without any issue.
Greg
2010-05-09 22:03:15