How to request a device token on an iphone

I can use the didRegisterForRemoteNotificationWithDeviceToken callback method to get my iphone's device token when subscribing to push notifications. My question is, how can I get this token again at a later time? When a user subscribes to something in their app, I want to send the device token and the ID of the item they subscribe to ... but I can't figure out where to get the device token from. I tried to use the uniqueIdentifer from the UIDevice class, but this value is different from the original original. I assumed I could call registerForRemoteNotificationTypes every time my app starts to issue a token. But if I do, I'm not surehow can i access this value from another class (my callRegisterForRemoteNotificationWithDeviceToken callback is in the main app's sub). Thanks for any help for C newbies!

+2


a source to share


1 answer


I would set a property in 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];
}

      



Then you can access this token using:

NSString * token = [(SomeAppDelegate*)[[UIApplication sharedApplication] delegate] dToken];

      

+5


a source







All Articles