问题描述
我陷入了如何在报告新来电之前结束所有上一次通话的问题,我的功能适用于2次通话,这意味着我可以在报告新来电之前结束第一个通话
这是我的代码:
// Handle incoming pushes
func pushRegistry(_ registry: PKPushRegistry,didReceiveIncomingPushWith payload: PKPushPayload,for type: PKPushType,completion: @escaping () -> Void) {
// print("Handle incoming pushes: \(payload.dictionaryPayload)")
endCall(thenReportNewCallForUuid: UUID.init())
}
func reportNewCall(uuid : UUID){
let config = CXProviderConfiguration(localizedname: "CallKitExample")
config.includesCallsInRecents = true
config.supportsVideo = true
config.supportedHandleTypes = [.generic]
config.iconTemplateImageData = UIImage(named: "logo_square")!.pngData()
config.maximumCallGroups = 1
config.maximumCallsPerCallGroup = 1
let provider = CXProvider(configuration: config)
provider.setDelegate(self,queue: nil)
let update = CXCallUpdate()
update.supportsHolding = false
update.supportsGrouping = false
update.supportsUngrouping = false
update.remoteHandle = CXHandle(type: .generic,value: uuid.uuidString)
update.hasVideo = true
provider.reportNewIncomingCall(with: uuid,update: update,completion: { error in
print("reportNewIncomingCall \(uuid) error: \(error)")
UserDefaults.standard.set(uuid.uuidString,forKey: "CallUUID")
UserDefaults.standard.synchronize()
})
}
func endCall(thenReportNewCallForUuid : UUID) {
guard let lastCallUUIDString = UserDefaults.standard.string(forKey: "CallUUID"),!lastCallUUIDString.isEmpty else{
return
}
print("end uuid: \(lastCallUUIDString)")
let call = UUID.init(uuidString: lastCallUUIDString)!
let controller = CXCallController()
let endTransaction = CXEndCallAction(call: call)
let transaction = CXTransaction(action: endTransaction)
controller.request(transaction,completion: { error in
if let error = error {
print("endcall Error: \(error)")
self.reportNewCall(uuid: thenReportNewCallForUuid)
} else {
print("endcall Success")
self.reportNewCall(uuid: thenReportNewCallForUuid)
}
})
}
这是我遇到的日志+错误
end uuid: CB91CCC6-7FCD-49D3-BE93-7A6581295B57
endcall Error: Error Domain=com.apple.CallKit.error.requesttransaction Code=2 "(null)"
-> OK first time endcall error because no call
reportNewIncomingCall 202DB031-23AE-46B6-91E9-3FBA708E07A7 error: nil
end uuid: 202DB031-23AE-46B6-91E9-3FBA708E07A7
endcall Success -> Matched call to end -> success
reportNewIncomingCall C45FEC0B-1320-4357-ADEF-7B7CA28D96C8 error: nil
end uuid: C45FEC0B-1320-4357-ADEF-7B7CA28D96C8
endcall Error: Error Domain=com.apple.CallKit.error.requesttransaction Code=4 "(null)"
-> Matched call to end -> Failed
reportNewIncomingCall CBDBA75A-B263-49E5-9138-8D5CCA28ED9E error: nil
标有重复的人请给出正确答案?谢谢
有人遇到同样的问题吗?请帮忙
解决方法
我在您的代码中至少看到了几个问题。但是,在此之前,为什么每次收到新的来电都结束正在进行的通话?我只是很好奇,因为这似乎不是很好的用户体验。
无论如何,我发现的问题如下:
- 在每个新来电中,您都会实例化一个新的
CXProvider
。如documentation中所述:
VoIP应用程序应仅创建一个CXProvider实例并将其存储以供全局使用。
- 您不会调用
pushRegistry(_:didReceiveIncomingPushWith:type:completion)
方法的完成处理程序。您应该在reportNewIncomingCall(with:update:completion:)
方法的完成处理程序中调用它。
我认为您遇到的错误是由CXProvider
问题引起的。但是,如果您还不解决第二个问题,则可能会引起另一个问题:系统将假定您尚未报告新的来话呼叫,因此,在几次呼叫后,它将停止向您发送新的VoIP推送( this limitation最早是在iOS 13中引入的。