ios Swift 3 - 设备令牌现在被解析为“32BYTES”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39495391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 10:08:39  来源:igfitidea点击:

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

iosswiftswift3

提问by user1537360

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'.

我刚刚从 Xcode 7 更新到 8 GM,在 Swift 3 兼容性问题中,我注意到我的设备令牌已停止工作。他们现在只读取“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.

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

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.

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

    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
}

回答by Rok Gregori?

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let token = deviceToken.map { String(format: "%02.2hhx", 
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)
}
) }.joined() print(token) }

回答by Oleg

I had the same problem. This is my solution:

我有同样的问题。这是我的解决方案:

extension Data {
    var hexString: String {
        return map { String(format: "%02.2hhx", arguments: [
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

   let token = String(data: deviceToken.base64EncodedData(), encoding: .utf8)?.trimmingCharacters(in: CharacterSet.whitespaces).trimmingCharacters(in: CharacterSet(charactersIn: "<>")) 
}
]) }.joined() } }

回答by phatmann

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

这是我的 Swift 3 扩展以获得 base-16 编码的十六进制字符串:

if #available(iOS 10.0, *) {
   let deviceTokenString = deviceToken.reduce("", {
deviceToken.base64EncodedString()
+ String(format: "%02X", )}) }

回答by rmaddy

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.

设备令牌从来都不是字符串,当然也不是 UTF-8 编码的字符串。是数据。它是 32 字节的不透明数据。

The only valid way to convert the opaque data into a string is to encode it - commonly through a base64 encoding.

将不透明数据转换为字符串的唯一有效方法是对其进行编码 - 通常通过 base64 编码。

In Swift 3/iOS 10, simply use the Data base64EncodedString(options:)method.

在 Swift 3/iOS 10 中,只需使用该Data base64EncodedString(options:)方法。

回答by user

Try this:

尝试这个:

let tokenData = deviceToken as NSData
let token = tokenData.description

// remove any characters once you have token string if needed
token = token.replacingOccurrences(of: " ", with: "")
token = token.replacingOccurrences(of: "<", with: ""
token = token.replacingOccurrences(of: ">", with: "")

回答by bluenowhere

try this

尝试这个

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

    let token = deviceToken.map({ String(format: "%02.2hhx", 
let token = String(format:"%@",deviceToken as CVarArg).components(separatedBy: CharacterSet.alphanumerics.inverted).joined(separator: "")
)}).joined() print("TOKEN: " + token) }

回答by Maselko

Swift 3

斯威夫特 3

The best and easiest way.

最好和最简单的方法。

let token = deviceToken.map { String(format: "%02.2hhx", ##代码##) }.joined()

回答by Bill Burgess

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.

这个没有作为官方答案(在评论中看到),而是我最终为了让我的令牌恢复正常所做的。

##代码##

回答by PacoG

##代码##

回答by Satheeshwaran

I just did this,

我就是这样做的

##代码##

it gave the result same as,

它给出了相同的结果,

##代码##