2026-07-28 22:34:27 +05:30
|
|
|
import Foundation
|
|
|
|
|
import Capacitor
|
|
|
|
|
import PushKit
|
|
|
|
|
import CallKit
|
|
|
|
|
import AVFoundation
|
2026-07-30 16:47:39 +05:30
|
|
|
import LiveKitClient // the CocoaPod is 'LiveKitClient' (no module_name), so the Swift module is LiveKitClient — NOT LiveKit
|
2026-07-28 22:34:27 +05:30
|
|
|
|
2026-07-30 16:15:43 +05:30
|
|
|
// Native calling for Biz Connect (iOS). Registered by `cap sync` as window.Capacitor.Plugins.NativeCall.
|
2026-07-28 22:34:27 +05:30
|
|
|
//
|
2026-07-30 16:15:43 +05:30
|
|
|
// ARCHITECTURE (native media):
|
|
|
|
|
// * PushKit: registers for VoIP pushes, reports the VoIP token to JS (-> /api/v1/devices 'ios-voip').
|
|
|
|
|
// * CallKit: incoming VoIP push -> full-screen system ring (works when force-killed). The CallKit call
|
|
|
|
|
// stays ACTIVE for the whole call (that's what foregrounds/unlocks the app on answer and keeps the call
|
|
|
|
|
// alive in the background).
|
|
|
|
|
// * LiveKit: the call MEDIA runs NATIVELY via the LiveKit iOS SDK — so the mic works with an active CallKit
|
|
|
|
|
// call (WebKit's WebRTC could not) and audio survives backgrounding. The VoIP payload carries the
|
|
|
|
|
// LiveKit url+token so we can connect immediately on answer, even from a killed state; outgoing calls get
|
|
|
|
|
// the token from the WebView via reportOutgoingCall(). The WebView is UI only for native calls (it must
|
|
|
|
|
// NOT also join the room — LiveKit allows one connection per identity).
|
2026-07-28 22:34:27 +05:30
|
|
|
@objc(NativeCallPlugin)
|
|
|
|
|
public class NativeCallPlugin: CAPPlugin, CAPBridgedPlugin, PKPushRegistryDelegate, CXProviderDelegate {
|
|
|
|
|
public let identifier = "NativeCallPlugin"
|
|
|
|
|
public let jsName = "NativeCall"
|
|
|
|
|
public let pluginMethods: [CAPPluginMethod] = [
|
|
|
|
|
CAPPluginMethod(name: "getToken", returnType: CAPPluginReturnPromise),
|
|
|
|
|
CAPPluginMethod(name: "reportOutgoingCall", returnType: CAPPluginReturnPromise),
|
2026-07-30 16:15:43 +05:30
|
|
|
CAPPluginMethod(name: "setMuted", returnType: CAPPluginReturnPromise),
|
2026-07-28 22:34:27 +05:30
|
|
|
CAPPluginMethod(name: "endCall", returnType: CAPPluginReturnPromise)
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
private var pushRegistry: PKPushRegistry?
|
|
|
|
|
private var provider: CXProvider?
|
|
|
|
|
private let callController = CXCallController()
|
|
|
|
|
private var voipToken: String = ""
|
2026-07-30 16:15:43 +05:30
|
|
|
// callUUID -> the call's data (room, kind, callerId, livekitUrl, livekitToken, …).
|
2026-07-28 22:34:27 +05:30
|
|
|
private var calls: [UUID: [String: Any]] = [:]
|
2026-07-29 17:54:21 +05:30
|
|
|
// Calls we've already ended locally — so a LATE cancel push doesn't re-report (which briefly re-rang).
|
|
|
|
|
private var endedCalls = Set<UUID>()
|
2026-07-30 16:15:43 +05:30
|
|
|
private var room: Room?
|
|
|
|
|
private var activeUUID: UUID?
|
2026-07-28 22:34:27 +05:30
|
|
|
|
|
|
|
|
override public func load() {
|
2026-07-28 23:03:01 +05:30
|
|
|
let config = CXProviderConfiguration(localizedName: "Biz Connect")
|
2026-07-28 22:34:27 +05:30
|
|
|
config.supportsVideo = true
|
|
|
|
|
config.maximumCallGroups = 1
|
|
|
|
|
config.maximumCallsPerCallGroup = 1
|
|
|
|
|
config.supportedHandleTypes = [.generic]
|
|
|
|
|
let p = CXProvider(configuration: config)
|
|
|
|
|
p.setDelegate(self, queue: nil)
|
|
|
|
|
provider = p
|
|
|
|
|
|
|
|
|
|
let registry = PKPushRegistry(queue: .main)
|
|
|
|
|
registry.delegate = self
|
|
|
|
|
registry.desiredPushTypes = [.voIP]
|
|
|
|
|
pushRegistry = registry
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-30 16:15:43 +05:30
|
|
|
// MARK: - LiveKit media
|
2026-07-28 22:34:27 +05:30
|
|
|
|
2026-07-30 16:15:43 +05:30
|
|
|
private func connectRoom(url: String, token: String) {
|
|
|
|
|
guard !url.isEmpty, !token.isEmpty else { return }
|
2026-07-30 17:51:46 +05:30
|
|
|
let old = room
|
2026-07-30 16:15:43 +05:30
|
|
|
let r = Room()
|
|
|
|
|
room = r
|
|
|
|
|
Task { [weak self] in
|
2026-07-30 17:51:46 +05:30
|
|
|
await old?.disconnect() // drop any previous/stale connection so we never leave DUPLICATE participants
|
2026-07-30 16:15:43 +05:30
|
|
|
do {
|
|
|
|
|
try await r.connect(url: url, token: token)
|
2026-07-31 10:46:13 +05:30
|
|
|
// House rule: answer MUTED. Not enabling the mic here means nothing is captured/published
|
|
|
|
|
// until the user taps Mic in the meeting UI (→ setMuted(false) → setMicrophone(true)), which
|
|
|
|
|
// is also when iOS asks for mic permission. Playback (hearing others) is unaffected.
|
|
|
|
|
try await r.localParticipant.setMicrophone(enabled: false)
|
2026-07-30 16:15:43 +05:30
|
|
|
self?.notifyListeners("callConnected", data: ["ok": true])
|
|
|
|
|
} catch {
|
|
|
|
|
self?.notifyListeners("callError", data: ["error": String(describing: error)])
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-07-28 22:34:27 +05:30
|
|
|
}
|
|
|
|
|
|
2026-07-30 16:15:43 +05:30
|
|
|
private func disconnectRoom() {
|
|
|
|
|
let r = room
|
|
|
|
|
room = nil
|
|
|
|
|
Task { await r?.disconnect() }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - JS-callable methods
|
|
|
|
|
|
|
|
|
|
@objc func getToken(_ call: CAPPluginCall) { call.resolve(["token": voipToken]) }
|
|
|
|
|
|
|
|
|
|
// Outgoing call from the web app: start a CallKit outgoing call AND connect the LiveKit room natively.
|
2026-07-28 22:34:27 +05:30
|
|
|
@objc func reportOutgoingCall(_ call: CAPPluginCall) {
|
|
|
|
|
guard let uuidStr = call.getString("callUUID"), let uuid = UUID(uuidString: uuidStr) else {
|
|
|
|
|
call.reject("callUUID required"); return
|
|
|
|
|
}
|
2026-07-30 16:15:43 +05:30
|
|
|
let url = call.getString("url") ?? ""
|
|
|
|
|
let token = call.getString("token") ?? ""
|
|
|
|
|
let video = call.getBool("hasVideo") ?? false
|
|
|
|
|
calls[uuid] = ["room": call.getString("room") ?? "", "kind": call.getString("kind") ?? "dm",
|
|
|
|
|
"livekitUrl": url, "livekitToken": token]
|
|
|
|
|
activeUUID = uuid
|
2026-07-28 22:34:27 +05:30
|
|
|
let handle = CXHandle(type: .generic, value: call.getString("peerName") ?? "Call")
|
|
|
|
|
let start = CXStartCallAction(call: uuid, handle: handle)
|
2026-07-30 16:15:43 +05:30
|
|
|
start.isVideo = video
|
|
|
|
|
callController.request(CXTransaction(action: start)) { [weak self] error in
|
|
|
|
|
if let error = error { call.reject(error.localizedDescription); return }
|
|
|
|
|
self?.connectRoom(url: url, token: token)
|
|
|
|
|
self?.provider?.reportOutgoingCall(with: uuid, connectedAt: Date())
|
|
|
|
|
call.resolve()
|
2026-07-28 22:34:27 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-30 16:15:43 +05:30
|
|
|
@objc func setMuted(_ call: CAPPluginCall) {
|
|
|
|
|
let muted = call.getBool("muted") ?? false
|
|
|
|
|
let r = room
|
|
|
|
|
Task { try? await r?.localParticipant.setMicrophone(enabled: !muted) }
|
2026-07-28 22:34:27 +05:30
|
|
|
call.resolve()
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-30 16:15:43 +05:30
|
|
|
// End the CallKit call (remote hung up / user ended from the web UI). No callUUID -> end all.
|
2026-07-28 22:34:27 +05:30
|
|
|
@objc func endCall(_ call: CAPPluginCall) {
|
|
|
|
|
if let uuidStr = call.getString("callUUID"), let uuid = UUID(uuidString: uuidStr) {
|
|
|
|
|
requestEnd(uuid)
|
|
|
|
|
} else {
|
|
|
|
|
for uuid in calls.keys { requestEnd(uuid) }
|
2026-07-30 16:15:43 +05:30
|
|
|
if let a = activeUUID { requestEnd(a) }
|
2026-07-28 22:34:27 +05:30
|
|
|
}
|
|
|
|
|
call.resolve()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func requestEnd(_ uuid: UUID) {
|
|
|
|
|
callController.request(CXTransaction(action: CXEndCallAction(call: uuid))) { _ in }
|
|
|
|
|
calls.removeValue(forKey: uuid)
|
2026-07-29 17:54:21 +05:30
|
|
|
endedCalls.insert(uuid)
|
2026-07-28 22:34:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - PushKit (VoIP)
|
|
|
|
|
|
|
|
|
|
public func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
|
|
|
|
|
let token = pushCredentials.token.map { String(format: "%02x", $0) }.joined()
|
|
|
|
|
voipToken = token
|
|
|
|
|
notifyListeners("voipToken", data: ["token": token])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func pushRegistry(_ registry: PKPushRegistry, didInvalidatePushTokenFor type: PKPushType) {
|
|
|
|
|
voipToken = ""
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-30 16:15:43 +05:30
|
|
|
// Incoming VoIP push. iOS 13+: we MUST reportNewIncomingCall for EVERY push before completion(), or the
|
|
|
|
|
// app is terminated.
|
2026-07-28 22:34:27 +05:30
|
|
|
public func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
|
2026-07-28 23:03:01 +05:30
|
|
|
var dict: [String: Any] = [:]
|
|
|
|
|
for (k, v) in payload.dictionaryPayload { if let ks = k as? String { dict[ks] = v } }
|
2026-07-28 22:34:27 +05:30
|
|
|
let uuid = UUID(uuidString: (dict["callUUID"] as? String) ?? "") ?? UUID()
|
2026-07-29 11:09:07 +05:30
|
|
|
|
2026-07-30 16:15:43 +05:30
|
|
|
// CANCEL: caller hung up / declined / timed out before answer. Still must report (or crash), so
|
|
|
|
|
// report then immediately end. If already reported the report errors (no re-ring); if relaunched it
|
|
|
|
|
// blips once then ends.
|
2026-07-29 11:09:07 +05:30
|
|
|
if (dict["type"] as? String) == "cancel" {
|
2026-07-29 22:48:24 +05:30
|
|
|
let u = CXCallUpdate()
|
|
|
|
|
u.remoteHandle = CXHandle(type: .generic, value: (dict["callerName"] as? String) ?? "Call")
|
|
|
|
|
calls.removeValue(forKey: uuid)
|
|
|
|
|
endedCalls.insert(uuid)
|
|
|
|
|
provider?.reportNewIncomingCall(with: uuid, update: u) { [weak self] _ in
|
|
|
|
|
self?.provider?.reportCall(with: uuid, endedAt: Date(), reason: .remoteEnded)
|
2026-07-29 17:54:21 +05:30
|
|
|
completion()
|
2026-07-29 11:09:07 +05:30
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-28 22:34:27 +05:30
|
|
|
let callerName = (dict["callerName"] as? String) ?? (dict["groupName"] as? String) ?? "Incoming call"
|
|
|
|
|
let hasVideo = (dict["hasVideo"] as? Bool) ?? false
|
|
|
|
|
calls[uuid] = dict
|
|
|
|
|
|
|
|
|
|
let update = CXCallUpdate()
|
|
|
|
|
update.remoteHandle = CXHandle(type: .generic, value: callerName)
|
|
|
|
|
update.localizedCallerName = callerName
|
|
|
|
|
update.hasVideo = hasVideo
|
|
|
|
|
update.supportsHolding = false
|
|
|
|
|
update.supportsGrouping = false
|
|
|
|
|
update.supportsUngrouping = false
|
|
|
|
|
|
2026-07-30 16:15:43 +05:30
|
|
|
provider?.reportNewIncomingCall(with: uuid, update: update) { _ in completion() }
|
2026-07-28 22:34:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - CXProviderDelegate
|
|
|
|
|
|
|
|
|
|
public func providerDidReset(_ provider: CXProvider) {
|
2026-07-30 16:15:43 +05:30
|
|
|
disconnectRoom()
|
|
|
|
|
calls.removeAll(); endedCalls.removeAll(); activeUUID = nil
|
2026-07-28 22:34:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {
|
2026-07-29 18:25:59 +05:30
|
|
|
let uuid = action.callUUID
|
2026-07-30 16:15:43 +05:30
|
|
|
let data = calls[uuid] ?? [:]
|
|
|
|
|
activeUUID = uuid
|
2026-07-28 22:34:27 +05:30
|
|
|
action.fulfill()
|
2026-07-30 16:15:43 +05:30
|
|
|
// Keep the CallKit call ACTIVE (foregrounds the app + keeps the call alive). Connect the LiveKit room
|
|
|
|
|
// NATIVELY so the mic works with the active CallKit call and audio survives backgrounding.
|
|
|
|
|
connectRoom(url: (data["livekitUrl"] as? String) ?? "", token: (data["livekitToken"] as? String) ?? "")
|
|
|
|
|
var ev = data; ev["callUUID"] = uuid.uuidString
|
2026-07-30 18:23:21 +05:30
|
|
|
// Answering from a KILLED/locked state launches the app — the WebView's JS listener may not be attached
|
|
|
|
|
// yet, so retain the event until it is. Without this the "answered" signal is lost, the server's
|
|
|
|
|
// unanswered timer fires, and the call is cancelled ~40s after pickup.
|
|
|
|
|
notifyListeners("answerCall", data: ev, retainUntilConsumed: true)
|
2026-07-28 22:34:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func provider(_ provider: CXProvider, perform action: CXEndCallAction) {
|
2026-07-28 23:03:01 +05:30
|
|
|
var data: [String: Any] = calls[action.callUUID] ?? [:]
|
2026-07-28 22:34:27 +05:30
|
|
|
data["callUUID"] = action.callUUID.uuidString
|
2026-07-30 16:15:43 +05:30
|
|
|
disconnectRoom()
|
2026-07-28 22:34:27 +05:30
|
|
|
notifyListeners("endCall", data: data)
|
|
|
|
|
calls.removeValue(forKey: action.callUUID)
|
2026-07-29 17:54:21 +05:30
|
|
|
endedCalls.insert(action.callUUID)
|
2026-07-30 16:15:43 +05:30
|
|
|
if activeUUID == action.callUUID { activeUUID = nil }
|
2026-07-28 22:34:27 +05:30
|
|
|
action.fulfill()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func provider(_ provider: CXProvider, perform action: CXStartCallAction) {
|
|
|
|
|
provider.reportOutgoingCall(with: action.callUUID, startedConnectingAt: Date())
|
|
|
|
|
action.fulfill()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func provider(_ provider: CXProvider, perform action: CXSetMutedCallAction) {
|
2026-07-30 16:15:43 +05:30
|
|
|
let r = room
|
|
|
|
|
Task { try? await r?.localParticipant.setMicrophone(enabled: !action.isMuted) }
|
2026-07-28 22:34:27 +05:30
|
|
|
notifyListeners("setMuted", data: ["callUUID": action.callUUID.uuidString, "muted": action.isMuted])
|
|
|
|
|
action.fulfill()
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-30 16:15:43 +05:30
|
|
|
// LiveKit's AudioManager manages the AVAudioSession by default and coordinates with CallKit; we just
|
|
|
|
|
// report the state to JS. (If the mic proves flaky, this is where we'd add manual AudioManager
|
|
|
|
|
// engine-availability coordination.)
|
2026-07-28 22:34:27 +05:30
|
|
|
public func provider(_ provider: CXProvider, didActivate audioSession: AVAudioSession) {
|
2026-07-28 23:03:01 +05:30
|
|
|
notifyListeners("audioActivated", data: ["ok": true])
|
2026-07-28 22:34:27 +05:30
|
|
|
}
|
|
|
|
|
public func provider(_ provider: CXProvider, didDeactivate audioSession: AVAudioSession) {
|
2026-07-28 23:03:01 +05:30
|
|
|
notifyListeners("audioDeactivated", data: ["ok": true])
|
2026-07-28 22:34:27 +05:30
|
|
|
}
|
|
|
|
|
}
|