deviceToken.map { String(format: "%02.2hhx", $0) }.joined(). In Objective‑C (pre‑iOS 13), use description and strip <, >, and spaces; on iOS 13+ use the same byte‑to‑hex approach via NSData enumeration.Why this happens
Apple Push Notification service (APNs) delivers the device token as raw binary data—NSData in Objective‑C, Data in Swift. It is not a UTF‑8 encoded string, so NSString stringWithUTF8String: or String(data:encoding:) returns nil or garbage. The token must be represented as a hexadecimal string (two characters per byte) before you send it to your push server.
The old Objective‑C workaround—calling description on NSData and stripping angle brackets and spaces—worked because NSData’s description happened to format bytes as <aa bb cc …>. That format is an implementation detail; Apple changed it in the iOS 13 SDK, so the same code now produces a different description and breaks.
How to fix it
1. Swift (iOS 13 and later) – preferred, type‑safe
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenString = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
print("APNs token: \(tokenString)")
// Send tokenString to your server
}
Data conforms to Sequence yielding UInt8 bytes. String(format: "%02.2hhx", byte) formats each byte as two lowercase hex digits. joined() concatenates them into the 64‑character string your backend expects.
2. Swift (legacy NSData API) – for very old codebases
If you still receive NSData (pre‑Swift 3 / iOS 10), bridge to Data first:
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let data = deviceToken as Data
let tokenString = data.map { String(format: "%02.2hhx", $0) }.joined()
print("APNs token: \(tokenString)")
}
3. Objective‑C (iOS 13+ SDK) – byte enumeration
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
const unsigned char *bytes = (const unsigned char *)[deviceToken bytes];
NSUInteger length = [deviceToken length];
NSMutableString *hexString = [NSMutableString stringWithCapacity:length * 2];
for (NSUInteger i = 0; i < length; i++) {
[hexString appendFormat:@"%02x", bytes[i]];
}
NSLog(@"APNs token: %@", hexString);
// Send hexString to your server
}
This avoids the fragile description format entirely and works on every iOS version.
4. Objective‑C (pre‑iOS 13 only) – description stripping (legacy)
// ⚠️ DO NOT USE ON iOS 13+ SDK
NSString *tokenString = [[[[deviceToken description]
stringByReplacingOccurrencesOfString:@"<" withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"APNs token: %@", tokenString);
Keep this only if you maintain an app that must build with an Xcode version older than Xcode 11. Otherwise migrate to the enumeration method above.
How to prevent it
- Always treat the device token as opaque binary data; never assume a string encoding.
- Use the byte‑to‑hex conversion in new code—it’s future‑proof and works across Swift and Objective‑C.
- Store the hex string on your server, not the raw bytes, to avoid encoding issues in databases or JSON.
- Test token handling on a physical device; the simulator does not receive real APNs tokens.
FAQ
Why does NSString stringWithUTF8String: return nil?
Because the token bytes are not valid UTF‑8. They are arbitrary 8‑bit values that only make sense as hexadecimal.
Can I use base64EncodedString instead of hex?
You can, but the de‑facto standard for APNs device tokens is lowercase hex without separators. Stick to hex unless your backend explicitly requires Base64.
Does the token change when the user reinstalls the app?
Yes. APNs may issue a new token after an app reinstall, OS update, or device restore. Always call registerForRemoteNotifications on launch and send the current token to your server.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.
