How to display a ViewController when answering a call with CallKit

北慕城南 提交于 2021-02-08 19:22:32

问题


I have followed the following tutorial to implement CallKit within my app:

https://www.raywenderlich.com/150015/callkit-tutorial-ios

But I would like to go further, and display my own ViewController while the call is active. I am doing a videocall service so I would like to have my own interface.

Is that possible at all? I have been trying to launch the ViewController from the method provider(CXProvider:CXAnswerCallAction) which is the one called when the user answers the call, but it seems to crash every time. I am trying to instantiate it with this (Swift 3):

let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "VideoCallViewController") as! VideoCallViewController
UIApplication.shared.keyWindow?.rootViewController?.present(vc, animated: true, completion: nil)

It crashes on the second line without explanation. It shows lldb, I have tried to get the backtrace by entering bt but it doesn't return anything.


回答1:


I figured it out:

let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = mainStoryboard.instantiateViewController(withIdentifier: "VideoCallViewController") as! VideoCallViewController

then, either:

vc.view.frame = UIScreen.main.bounds
UIView.transition(with: self.window!, duration: 0.5, options: .transitionCrossDissolve, animations: {
    self.window!.rootViewController = vc
}, completion: nil)

or:

self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = vc
self.window?.makeKeyAndVisible()

According to https://stackoverflow.com/a/35226874/5798668, the first option is preferable because if you use the second one you will have multiple UIWindows active in your app at the same time.

NOTE: In my case the ProviderDelegate did not have a self.window attribute, this was passed to it by the AppDelegate.swift, in which a Push notification was executing the reportIncomingCall() of the delegate.



来源:https://stackoverflow.com/questions/44590971/how-to-display-a-viewcontroller-when-answering-a-call-with-callkit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!