I just updated from Xcode 7 to the 8 GM and amidst the Swift 3 compatibility issues I noticed that my device tokens have stopped working. They now only read '32BYTES'.
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
print(deviceToken) // Prints '32BYTES'
print(String(data: deviceToken , encoding: .utf8)) // Prints nil
}
Before the update I was able to simply send the NSData to my server, but now I'm having a hard time actually parsing the token.
What am I missing here?
Edit: I just testing converting back to NSData and I'm seeing the expected results. So now I'm just confused about the new Data type.
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
print(deviceToken) // Prints '32BYTES'
print(String(data: deviceToken , encoding: .utf8)) // Prints nil
let d = NSData(data: deviceToken)
print(d) // Prints my device token
}
I just did this,
it gave the result same as,
try this
The device token has never been a string and certainly not a UTF-8 encoded string. It's data. It's 32 bytes of opaque data.
The only valid way to convert the opaque data into a string is to encode it - commonly through a base64 encoding.
In Swift 3/iOS 10, simply use the
Data base64EncodedString(options:)
method.This one wasn't stated as an official answer (saw it in a comment), but is what I ultimately did to get my token back in order.
The best and easiest way.