Swift 3 - device tokens are now being parsed as '32BYTES'

Solution 1:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
    print(token)
}

Solution 2:

I had the same problem. This is my solution:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    var token = ""
    for i in 0..<deviceToken.count {
        token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]])
    }
    print(token)
}

Solution 3:

Here is my Swift 3 extension to get a base-16 encoded hex string:

extension Data {
    var hexString: String {
        return map { String(format: "%02.2hhx", arguments: [$0]) }.joined()
    }
}

Solution 4:

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.