Swift 3 - 设备令牌现在被解析为“32BYTES" [英] Swift 3 - device tokens are now being parsed as '32BYTES'

查看:11
本文介绍了Swift 3 - 设备令牌现在被解析为“32BYTES"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚从 Xcode 7 更新到 8 GM,在 Swift 3 兼容性问题中,我注意到我的设备令牌已停止工作.他们现在只能读取32BYTES".

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
}

在更新之前,我能够简单地将 NSData 发送到我的服务器,但现在我很难实际解析令牌.

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.

我在这里遗漏了什么?

我只是测试转换回 NSData 并且我看到了预期的结果.所以现在我只是对新的数据类型感到困惑.

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
}

推荐答案

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

这篇关于Swift 3 - 设备令牌现在被解析为“32BYTES"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆