代码之家  ›  专栏  ›  技术社区  ›  Piepants

您应该如何将推送令牌数据转换为用于查看/调试/记录/放入http的字符串以发送到服务器?

  •  0
  • Piepants  · 技术社区  · 5 年前

    使用XCode 11,我不再能够查看推送令牌的完整值。下面是一些示例代码来说明:

    func pushRegistry(_ registry: PKPushRegistry, didUpdate credentials: PKPushCredentials, for type: PKPushType)
    {
        var method1 = NSString(format: "%@", credentials.token as CVarArg) as String
        print("method1: \(method1)")
    
        method1 = method1.replacingOccurrences(of: " ", with: "")
        method1 = method1.replacingOccurrences(of: "<", with: "")
        method1 = method1.replacingOccurrences(of: ">", with: "")
        print("method1 again: \(method1)")
    
        let method2 = String(decoding: credentials.token, as: UTF8.self)
        print("method2: \(String(describing: method2))")
    
        let method3 = credentials.token.description as String
        print("method3: \(method3)")
    

    但是,当以上代码与Xcode 11一起运行时,输出如下:

    method1: {length = 32, bytes = 0x5b3f44e0 6d2c5ee5 5252d3db f5bb915b ... 12844aeb 13259e7e }
    method1 again: {length=32,bytes=0x5b3f44e06d2c5ee55252d3dbf5bb915b...12844aeb13259e7e}
    method2: [?D�m,^�RR�����[����>��J�%�~
    method3: 32 bytes
    

    查看Xcode中的变量时:

    enter image description here

    在早期版本的Xcode中,method1将被记录/查看,如下所示:

    44154da73234500153106883ffc1071fa59c0d24f1a1d29ea70871e5aa8dbb41
    

    0x5b3f44e06d2c5ee55252d3dbf5bb915b...12844aeb13259e7e
    

    有。。。就在中间。

    我想将值复制/粘贴到一个php脚本中,以手动向应用程序发送push消息以进行测试。

    0 回复  |  直到 5 年前
        1
  •  2
  •   7stud    5 年前

    也许可以尝试打印每个字符,而不是整个字符串:

    let str = "44154da73234500153106883ffc1071fa59c0d24f1a1d29ea70871e5aa8dbb41"
    let tokenData = Data(str.utf8)
    
    let tokenStr = String(decoding: tokenData, as: UTF8.self)
    //Supposedly, that is a failable String initializer and therefore it returns an optional type.
    
    print(str)
    if let tokenStr = tokenStr {
        for c in tokenStr {
            print(c, terminator: "")
    
        }
    }
    

    输出:

    44154da73234500153106883ffc1071fa59c0d24f1a1d29ea70871e5aa8dbb41  
    44154da73234500153106883ffc1071fa59c0d24f1a1d29ea70871e5aa8dbb41
    
        2
  •  2
  •   Piepants    5 年前

    这个

        let tokenParts = credentials.token.map { data in String(format: "%02.2hhx", data) }
        let token = tokenParts.joined()
        print("Device Token: \(token)")