Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving the DeviceToken for Later Use in Apple Push Notification Services

In my iPhone app I am getting the device token from Apple which I am assigning a public property inside the Delegate file as shown below:

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
   self.dToken = [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding]; 
}

The dToken property is declared as shown below:

NSString *dToken;

@property (nonatomic,retain) NSString *dToken;

But when I try to retrieve the device token from another file I get the null value.

+(NSString *) getDeviceToken
{
  NSString *deviceToken = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] dToken];

    NSLog(@" getDeviceToken = %@",deviceToken);  // This prints NULL

    return deviceToken; 

}

What am I doing wrong?

like image 856
azamsharp Avatar asked Sep 29 '11 17:09

azamsharp


2 Answers

I suggest you to convert token to string in this way:

self.dToken = [[[deviceToken description]
                    stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] 
                    stringByReplacingOccurrencesOfString:@" " 
                    withString:@""];

UPDATED: As many people mentioned it is better to use next approach to convert NSData * to NSString *:

@implementation NSData (Conversion)
- (NSString *)hexadecimalString
{
  const unsigned char *dataBuffer = (const unsigned char *)[self bytes];

  if (!dataBuffer) {
    return [NSString string];
  }

  NSUInteger          dataLength  = [self length];
  NSMutableString     *hexString  = [NSMutableString stringWithCapacity:(dataLength * 2)];

  for (int i = 0; i < dataLength; ++i) {
    [hexString appendFormat:@"%02lx", (unsigned long)dataBuffer[i]];
  }

  return hexString;
}
@end
like image 192
Nekto Avatar answered Sep 22 '22 16:09

Nekto


From the discussion at Best way to serialize an NSData into a hexadeximal string, here is a better way to do it. Is longer, but your code will be future-proof if Apple changes the way NSData emit debugger descriptions.

Extend NSData as follows:

@implementation NSData (Hex)
- (NSString*)hexString {
    unichar* hexChars = (unichar*)malloc(sizeof(unichar) * (self.length*2));
    unsigned char* bytes = (unsigned char*)self.bytes;
    for (NSUInteger i = 0; i < self.length; i++) {
        unichar c = bytes[i] / 16;
        if (c < 10) c += '0';
        else c += 'A' - 10;
        hexChars[i*2] = c;
        c = bytes[i] % 16;
        if (c < 10) c += '0';
        else c += 'A' - 10;
        hexChars[i*2+1] = c;
    }
    NSString* retVal = [[NSString alloc] initWithCharactersNoCopy:hexChars
                                                           length:self.length*2 
                                                     freeWhenDone:YES];
    return [retVal autorelease];
}
@end
like image 28
Juan Carlos Méndez Avatar answered Sep 23 '22 16:09

Juan Carlos Méndez