iOS使用CXCallObserver监听电话接听与拨打

初始化

import CallKit

class HomeViewController: UIViewController, CXCallObserverDelegate {
    public var cacheManager: TripCacheManager = TripCacheManager.init()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.callObserver.setDelegate(self, queue: DispatchQueue.main)
    }
}

状态监听

func callObserver(_ callObserver: CXCallObserver, callChanged call: CXCall) {
    print("call observer uuid: \(call.uuid)")
    print("outgoing(拨打):\(call.isOutgoing) onHold(待接通):\(call.isOnHold) hasConnected(接通):\(call.hasConnected) hasEnded(挂断):\(call.hasEnded)")
    }
}

该方法不能通过单一状态来判断当前状态,建议进行多值的判断。

取消监听

self.callObserver.setDelegate(nil, queue: DispatchQueue.main)

你可能感兴趣的:(iOS,ios)