美文网首页
【Swift】PushKit(voip开发)--之获取pushT

【Swift】PushKit(voip开发)--之获取pushT

作者: 哗啦啦啦呃 | 来源:发表于2022-12-18 15:18 被阅读0次

    记录一下我改的一个关于获取的pushToken在iOS15上能生效,但是在iOS16上不生效的bug

    首先,获取token是在 pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {} 代理方法中的。

    原获取方法:

    func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
            print("voip 应用启动此代理方法会返回设备Token")
            guard type == PKPushType.voIP else {
                print("voip type is not voIP")
                return
            }
            if pushCredentials.token.count <= 0 {
                print("voip token nil")
                return
            }
    var hexString : String = UnsafeBufferPointer<UInt8>(start: UnsafePointer(pushCredentials.token.bytes),
                                                                count: pushCredentials.token.count).map { String(format: "%02x", $0) }.joined(separator: "")
            hexString = hexString.replacingOccurrences(of: "<", with: "")
            hexString = hexString.replacingOccurrences(of: ">", with: "")
            hexString = hexString.replacingOccurrences(of: " ", with: "")
            print("voip token1 = \(hexString)")
    }
    

    这样获取在iOS15之前是有用的,但是因为iOS16对UnsafePointer(不安全指针)方法应该是有所修改,导致最后得不到正确的token,voip也就没办法推送了。
    所以需要token转字符串的方法

    新的方法:

    func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
            print("voip 应用启动此代理方法会返回设备Token")
            guard type == PKPushType.voIP else {
                print("voip type is not voIP")
                return
            }
            if pushCredentials.token.count <= 0 {
                print("voip token nil")
                return
            }
            
            var hexString = pushCredentials.token.bytes.map{ String(format: "%02x", $0) }.joined(separator: "")
            hexString = hexString.replacingOccurrences(of: "<", with: "")
            hexString = hexString.replacingOccurrences(of: ">", with: "")
            hexString = hexString.replacingOccurrences(of: " ", with: "")
            print("voip token1 = \(hexString)")
        }
    

    Over

    相关文章

      网友评论

          本文标题:【Swift】PushKit(voip开发)--之获取pushT

          本文链接:https://www.haomeiwen.com/subject/jcronrtx.html