Properly handle CallKit termination when calls end

This commit is contained in:
QuentinArguillere
2020-08-06 15:14:49 +02:00
parent f723655265
commit 69b7a7f447
3 changed files with 53 additions and 33 deletions

View File

@@ -37,7 +37,10 @@ class CallExampleContext : ObservableObject
@Published var passwd : String = "peche5"
@Published var loggedIn: Bool = false
var providerDelegate : CallKitProviderDelegate!
var mProviderDelegate : CallKitProviderDelegate!
let outgoingCallName = "Outgoing call example"
let incomingCallName = "Incoming call example"
@Published var enableCallKit = false;
init()
@@ -67,7 +70,7 @@ class CallExampleContext : ObservableObject
mCore.addDelegate(delegate: mCallStateTracer)
mCore.addDelegate(delegate: mRegistrationDelegate)
providerDelegate = CallKitProviderDelegate(context : self)
mProviderDelegate = CallKitProviderDelegate(context : self)
}
func registrationExample()
@@ -95,19 +98,25 @@ class CallExampleContext : ObservableObject
}
}
func createCallParams() throws -> CallParams
{
let callParams = try mCore.createCallParams(call: nil)
callParams.videoEnabled = videoEnabled;
callParams.audioEnabled = audioEnabled;
return callParams
}
// Initiate a call
func outgoingCallExample()
{
do {
let callParams = try mCore.createCallParams(call: nil)
callParams.videoEnabled = videoEnabled;
callParams.audioEnabled = audioEnabled;
if (!callRunning)
{
let callDest = try Factory.Instance.createAddress(addr: dest)
// Place an outgoing call
mCall = mCore.inviteAddressWithParams(addr: callDest, params: callParams)
mCall = mCore.inviteAddressWithParams(addr: callDest, params: try createCallParams())
if (mCall == nil) {
print("Could not place call to \(dest)\n")
@@ -117,7 +126,7 @@ class CallExampleContext : ObservableObject
}
else
{
try mCall.update(params: callParams)
try mCall.update(params: createCallParams())
}
} catch {
print(error)
@@ -125,12 +134,6 @@ class CallExampleContext : ObservableObject
}
// Initiate a call
func outgoingCallKitCallExample()
{
providerDelegate.outgoingCall(uuid: UUID())
}
// Terminate a call
func stopCall()
{
@@ -139,7 +142,14 @@ class CallExampleContext : ObservableObject
// terminate the call
print("Terminating the call...\n")
do {
try mCall.terminate()
if (enableCallKit)
{
mProviderDelegate.stopCall()
}
else
{
try mCall.terminate()
}
} catch {
print(error)
}
@@ -220,15 +230,14 @@ class CallStateDelegate: CoreDelegate {
if (cstate == .IncomingReceived) {
tutorialContext.mCall = call
tutorialContext.isCallIncoming = true
if (tutorialContext.enableCallKit)
{
tutorialContext.providerDelegate.reportIncomingCall(call: call, uuid: UUID(), handle: "Incoming Test Call", hasVideo: false)
}
if (tutorialContext.enableCallKit) { tutorialContext.mProviderDelegate.incomingCall() }
} else if (cstate == .OutgoingRinging) {
tutorialContext.callRunning = true
} else if (cstate == .End) {
tutorialContext.callRunning = false
tutorialContext.isCallIncoming = false;
if (tutorialContext.enableCallKit) { tutorialContext.mProviderDelegate.stopCall() }
}
}
}

View File

@@ -17,6 +17,9 @@ class CallKitProviderDelegate : NSObject
private let provider: CXProvider
let mCallController = CXCallController()
var tutorialContext : CallExampleContext!
var incomingCallUUID : UUID!
var outgoingCallUUID : UUID!
init(context : CallExampleContext)
{
@@ -34,27 +37,32 @@ class CallKitProviderDelegate : NSObject
}
func reportIncomingCall(call:Call?, uuid: UUID, handle: String, hasVideo: Bool) {
func incomingCall() {
incomingCallUUID = UUID()
let update = CXCallUpdate()
update.remoteHandle = CXHandle(type:.generic, value: handle)
update.hasVideo = hasVideo
update.remoteHandle = CXHandle(type:.generic, value: tutorialContext.incomingCallName)
update.hasVideo = tutorialContext.videoEnabled
provider.reportNewIncomingCall(with: uuid, update: update) { error in
}
provider.reportNewIncomingCall(with: incomingCallUUID, update: update, completion: { error in })
}
func outgoingCall(uuid : UUID)
func outgoingCall()
{
let handle = CXHandle(type: .generic, value: "Outgoing Call")
let startCallAction = CXStartCallAction(call: uuid, handle: handle)
outgoingCallUUID = UUID()
let handle = CXHandle(type: .generic, value: tutorialContext.outgoingCallName)
let startCallAction = CXStartCallAction(call: outgoingCallUUID, handle: handle)
let transaction = CXTransaction(action: startCallAction)
mCallController.request(transaction, completion: { error in
print("lalalalala")
})
mCallController.request(transaction, completion: { error in })
}
func stopCall()
{
let endCallAction = CXEndCallAction(call: incomingCallUUID)
let transaction = CXTransaction(action: endCallAction)
mCallController.request(transaction, completion: { error in })
}
}
@@ -62,7 +70,10 @@ class CallKitProviderDelegate : NSObject
extension CallKitProviderDelegate: CXProviderDelegate {
func provider(_ provider: CXProvider, perform action: CXEndCallAction) {
tutorialContext.stopCall()
if (tutorialContext.mCall.state != Call.State.End)
{
try? tutorialContext.mCall.terminate()
}
action.fulfill()
}

View File

@@ -143,7 +143,7 @@ struct ContentView: View {
}
VStack(alignment: .leading) {
Button(action: tutorialContext.outgoingCallKitCallExample) {
Button(action: tutorialContext.mProviderDelegate.outgoingCall) {
Text("CallKit Call")
.font(.largeTitle)
.foregroundColor(Color.white)