Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

swift3

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 } 
like image 519
user1537360 Avatar asked Sep 14 '16 16:09

user1537360


2 Answers

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {     let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()     print(token) } 
like image 194
Rok Gregorič Avatar answered Sep 21 '22 02:09

Rok Gregorič


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) } 
like image 41
Oleg Avatar answered Sep 20 '22 02:09

Oleg