Cotton code
61 lines · 2.2 KB · SwiftRaw
// The system's call UI on a simulator: none. CallKit hangs up an outgoing call// on a simulator about a second after starting it (it cannot open its in-call// screen there: "Failed to open URL facetime://?showInCallUI=1"), and the// session is then never activated, so a call placed or joined from a// simulator could not stay up (seen walking a group call on dev, launch-ios,// 23 Nov 2026). Here a call starts, connects and ends in the app alone, the// audio session activated by this port as CallKit would. A simulator gets no// VoIP push, so it is never rung; a real iPhone always has CallKit.#if os(iOS) && targetEnvironment(simulator)import AVFoundationimport Foundation@MainActorpublic final class SimulatorCallSystem: SystemCallPort {    /// Where the session's activation goes: set once the center is made.    public weak var center: CallCenter?    private var live: Set<UUID> = []    private let log: @Sendable (String) -> Void    public init(log: @escaping @Sendable (String) -> Void = { _ in }) {        self.log = log    }    public func startOutgoing(uuid: UUID, handle: String, name: String?, video: Bool) {        live.insert(uuid)        activate()    }    public func outgoingConnecting(uuid: UUID) {}    public func outgoingConnected(uuid: UUID) {}    public func reportIncoming(uuid: UUID, title: String, handle: String?, video: Bool) {        // Answered from the app's own screen: the session is the app's to start.        live.insert(uuid)        activate()    }    public func update(uuid: UUID, handle: String, name: String?, video: Bool) {}    public func ended(uuid: UUID, reason: Int?) {        guard live.remove(uuid) != nil, live.isEmpty else { return }        center?.audioDeactivated()        do {            try AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation)        } catch {            log("simulator call: the session would not stop: \(error)")        }    }    private func activate() {        do {            try AVAudioSession.sharedInstance().setActive(true)            center?.audioActivated()        } catch {            log("simulator call: the session would not start: \(error)")        }    }}#endif