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

前端 未结 11 2564
广开言路
广开言路 2020-12-13 01:27

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

相关标签:
11条回答
  • 2020-12-13 02:00

    try this

    if #available(iOS 10.0, *) {
       let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
    }
    
    0 讨论(0)
  • 2020-12-13 02:05

    Try this:

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    
       let token = String(data: deviceToken.base64EncodedData(), encoding: .utf8)?.trimmingCharacters(in: CharacterSet.whitespaces).trimmingCharacters(in: CharacterSet(charactersIn: "<>")) 
    }
    
    0 讨论(0)
  • 2020-12-13 02:06

    I just did this,

    let token = String(format:"%@",deviceToken as CVarArg).components(separatedBy: CharacterSet.alphanumerics.inverted).joined(separator: "")
    

    it gave the result same as,

    let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
    
    0 讨论(0)
  • 2020-12-13 02:09

    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()
        }
    }
    
    0 讨论(0)
  • 2020-12-13 02:10

    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)
    }
    
    0 讨论(0)
提交回复
热议问题