为 Pushkit 分配 PKPushRegistryDelegate 时如何消除类型错误?

How to remove type error when assigning PKPushRegistryDelegate for Pushkit?

我正在尝试创建一个 VOIP 呼叫应用程序,但收到“无法将 'CallDelegate.Type' 的值分配给 'PKPushRegistryDelegate。

下面是我的代表class。 class 继承了 PKPushRegistryDelegate 并实现了 pushRegistry 方法。不在 AppDelegate 中,因为我需要 Init 方法来初始化 CallKit CXProvider(同样的错误,如果你把所有东西都放在 AppDelegate 中)。

import Foundation
import CallKit
import PushKit

class CallDelegate : NSObject, PKPushRegistryDelegate
{
    private let provider: CXProvider        

    static func registerVoIPPush() {
        let voipPushResgistry = PKPushRegistry(queue: DispatchQueue.main)

        voipPushResgistry.delegate = self //**Getting the assign error here**

        voipPushResgistry.desiredPushTypes = [PKPushType.voIP]            
    }

    func pushRegistry( _ registry: PKPushRegistry, didUpdate credentials: PKPushCredentials, for type: PKPushType) {
        if type == PKPushType.voIP {                
            let tokenData = credentials.token                
            let voipPushToken = String(data: tokenData, encoding: .utf8)

            //Send the POST request to oneSignal here
        }
    }

    func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType) {
        if type == .voIP {

            if let handle = payload.dictionaryPayload["handle"] as? String,
                let uuidString = payload.dictionaryPayload["uuid"] as? String,
                let isVideo = payload.dictionaryPayload["isVideo"] as? Bool {

                reportInComingCallWith(uuidString: uuidString, handle: handle, isVideo: isVideo)
            }
        }
    }


    func pushRegistry( _ registry: PKPushRegistry, didInvalidatePushTokenFor type: PKPushType) {            
    }

    func pushRegistry( _ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type:PKPushType, completion: @escaping () -> Void) {            
        if type == PKPushType.voIP {                
        }
    }

我在功能中启用了 VOIP 通知并将 Pushkit 框架链接到项目。目标 IOS 13. 文档中的很多东西都不是开箱即用的(不推荐使用的方法、名称等),也许注册表分配也发生了变化。

问题是您的 registerVoIPPush() 方法是静态的,因此在该上下文中 self 指的是类型而不是 CallDelegate 的实例。您必须将 CallDelegate class 的实例分配给 PKPushRegistry 委托。