Device token parsing in Objective C

Hello,

Our used cases is VoIP calling.

There are multiple answers available on internet on how a device token should be parsed.

We are using following technique to parse device token, let us know if there is an Apple Recommended way of parsing device token -

Code Block objc
+ (NSString *)deviceTokenWithData:(NSData *)data {
  if (![data isKindOfClass:[NSData class]] !([data length] > 0)) {
    TVOLogError(@"Invalid device token");
    return nil;
  }
  const char *tokenBytes = (const char *)[data bytes];
  NSMutableString *deviceTokenString = [NSMutableString string];
  for (NSUInteger i = 0; i < [data length]; i) {
    [deviceTokenString appendFormat:@"%02.2hhx", tokenBytes[i]];
  }
   
  return deviceTokenString;
}


Also, it would be nice if apple provides a toString method to get string out of the device token raw bytes.

Replies

Hi ptank!

Per our primer on push notifications posted this year, the recommended way of getting a string from a token is as follows

Code Block Swift
let tokenComponents = token.map { data in String(format: "%02.2hhx", data) }
let deviceTokenString = tokenComponents.joined()


If you have any other questions, please let us know!
Hello - Thanks for the swift code snippet. My question is mainly for Objective-C as we parse token in our SDK which is written in Objective-C.

Best,
Piyush
Any recommended guideline for Objective C token parsing ?