单击CallKit中的应用程序图标时打开应用程序

时间:2018-10-08 10:47:23

标签: swift sinch callkit

我使用SINCH SDK集成了语音呼叫,我的问题是电话处于锁定屏幕上,并且我通过CallKit收到来电,并且可以接听所有问题,但是当我按应用程序图标时,该应用程序在VoiceCallController中打开并运行电话再次响起铃声,有人可以帮我吗?SINCallKitProvider类:NSObject,CXProviderDelegate {

var _client: SINClient!
var _provider: CXProvider!
var _acDelegate: AudioContollerDelegate!
var _calls: [UUID : SINCall]
var _muted: Bool


init(withClient: SINClient) {

    _client = withClient
    _muted = false
    _acDelegate = AudioContollerDelegate()
    _client.audioController().delegate = _acDelegate
    _calls = [:]

    let config = CXProviderConfiguration(localizedName: "ok")
    config.maximumCallGroups = 2
    config.supportsVideo = false
    config.maximumCallsPerCallGroup = 1
    let callkitIcon = UIImage(named: "ok")
    config.iconTemplateImageData = UIImagePNGRepresentation(callkitIcon!)

    _provider = CXProvider(configuration: config)


    super.init()

    _provider.setDelegate(self, queue: nil)

    NotificationCenter.default.addObserver(self, selector: #selector(callDidEnd), name: NSNotification.Name(rawValue: "SINCallDidEndNotification"), object: nil)

}

func reportNewIncomingCall (call: SINCall) {
    let firstName = currentUser.string(forKey: "firstName")
    let lastName = currentUser.string(forKey: "lastName")
    let fullName = "\(firstName!) \(lastName!)"

    var caller = fullName
    if let call = call.headers["from"] {
        caller = call as! String
    }

    let update = CXCallUpdate()
    update.remoteHandle = CXHandle(type: .generic, value: caller)

    _provider.reportNewIncomingCall(with: UUID(uuidString: call.callId)!, update: update) { (error) in
        if error != nil {
            print("error call \(error!.localizedDescription)")
            return
        }

        self.addNewCall(call: call)
    }

}

func addNewCall(call: SINCall) {
    print("Added call \(call.callId)")
    _calls[UUID(uuidString: call.callId)!] = call
}



// Handle cancel/bye event initiated by either caller or callee
@objc func callDidEnd(notification: Notification) {

    if let call: SINCall = notification.userInfo![SINCallKey] as? SINCall {

        let cause = SINGetCallEndedReason(cause: call.details.endCause)

        _provider.reportCall(with: UUID(uuidString: call.callId)!, endedAt: call.details.endedTime, reason: cause)


        if self.callExist(callId: call.callId) {
            print("CallDidEnd, removing \(call.callId)")
            _calls.removeValue(forKey: UUID(uuidString: call.callId)!)
        }

    } else {
        print("****warning no call was reported")
    }


}


func callExist (callId: String) -> Bool {

    if _calls.count == 0 {
        return false
    }

    for callKitCall in _calls.values {
        if callKitCall.callId == callId {
            return true
        }
    }
    return false
}

func activeCalls() -> [SINCall] {
    return Array(_calls.values)
}

func currentEstablishedCall () -> SINCall? {
    let calls = activeCalls()

    if calls.count == 1 && calls[0].state == SINCallState.established {
        return calls[0]
    } else {
        return nil
    }
}


//MARK: CXProvider delegate
func provider(_ provider: CXProvider, didActivate audioSession: AVAudioSession) {
    print("Did activate")
    _client.call()?.provider(provider, didActivate: audioSession)
}

func callForAction(action: CXCallAction) -> SINCall? {

    let call = _calls[action.callUUID]
    if call == nil {
        print("Warning no call found for action \(action.callUUID)")
        return nil
    }
    return call
}

func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) {
    print("Answer call action")
    callForAction(action: action)!.answer()
    action.fulfill()
}

func provider(_ provider: CXProvider, perform action: CXEndCallAction) {
    print("end call action")
    callForAction(action: action)!.hangup()
    action.fulfill()
}

func provider(_ provider: CXProvider, perform action: CXSetMutedCallAction) {
    print("mute call action")

    if _acDelegate.muted {
        _client.audioController().unmute()
    } else {
        _client.audioController().mute()
    }
    action.fulfill()
}

func provider(_ provider: CXProvider, didDeactivate audioSession: AVAudioSession) {
    print("did diactivate audio session")
}


func providerDidReset(_ provider: CXProvider) {
    print("did reset")

}


//MARK: Helpers
func SINGetCallEndedReason(cause: SINCallEndCause) -> CXCallEndedReason {
    switch cause {
    case .error:
        return CXCallEndedReason.failed
    case.denied:
        return CXCallEndedReason.remoteEnded
    case .hungUp:
        return CXCallEndedReason.remoteEnded
    case .timeout:
        return CXCallEndedReason.unanswered
    case .canceled:
        return CXCallEndedReason.unanswered
    case .noAnswer:
        return CXCallEndedReason.unanswered
    case .otherDeviceAnswered:
        return CXCallEndedReason.unanswered
    default:
        break
    }

    return CXCallEndedReason.failed
}

}`

1 个答案:

答案 0 :(得分:1)

您是说通过CallKit锁定屏幕UI上的应用程序图标进入应用程序时,应用程序显示的视图不正确吗?

如果这是您的问题,一个解决方案可能是:您应该在callkit提供程序中维护一个列表或变量,仅用于检查是否有已接通的呼叫。并且您的应用程序代表应侦听applicationWillEnterForeground通知,如果用户单击callkit UI上的应用程序图标,则会触发该通知。

当收到applicationWillEnterForeground通知时,请检查呼叫包提供程序是否正在保留任何活动呼叫,如果是,则显示显示正在进行呼叫的视图,否则显示将在当前实现中显示的任何视图。

>

SinchCallKit示例应用程序为处理此问题提供了一些实现参考(尽管在目标c中)。请检查该项目中的AppDelegate.m,CallViewController.m和SINCallKitProvider.m。