使用 CallKit 接听电话时如何显示 ViewController [英] How to display a ViewController when answering a call with CallKit
问题描述
我已按照以下教程在我的应用中实现 CallKit:
I have followed the following tutorial to implement CallKit within my app:
https://www.raywenderlich.com/150015/callkit-tutorial-ios
但我想更进一步,并在调用处于活动状态时显示我自己的 ViewController.我正在做视频通话服务,所以我想拥有自己的界面.
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.
这可能吗?我一直在尝试从方法 provider(CXProvider:CXAnswerCallAction)
启动 ViewController,该方法是用户接听电话时调用的方法,但似乎每次都崩溃.我试图用这个(Swift 3)实例化它:
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)
它在没有解释的情况下在第二行崩溃.它显示 lldb
,我试图通过输入 bt
来获取回溯,但它没有返回任何内容.
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.
推荐答案
我想通了:
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = mainStoryboard.instantiateViewController(withIdentifier: "VideoCallViewController") as! VideoCallViewController
那么,要么:
vc.view.frame = UIScreen.main.bounds
UIView.transition(with: self.window!, duration: 0.5, options: .transitionCrossDissolve, animations: {
self.window!.rootViewController = vc
}, completion: nil)
或:
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = vc
self.window?.makeKeyAndVisible()
根据 https://stackoverflow.com/a/35226874/5798668,第一个选项更可取,因为如果如果您使用第二个,您的应用程序中将同时有多个活动的 UIWindows.
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.
注意:在我的例子中 ProviderDelegate
没有 self.window
属性,这是由 AppDelegate.swift
传递给它的,其中推送通知正在执行委托的 reportIncomingCall()
.
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.
这篇关于使用 CallKit 接听电话时如何显示 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!