在AppDelegate中,无论何时收到VoIP调用(推送通知),都会调用以下函数,从而创建“VideoCallViewController”的多个实例
我使用了deinit(在VideoCallViewController中),如下所示,检查在创建“VideoCallViewController”的新实例之前是否先初始化了“VideoCallViewController”的前一个实例,令我惊讶的是没有调用print("Deinitializing VC)
,实例在内存中。
如果AppDelegate中已经存在VideoCallViewController的实例,如何显示VideoCallViewController及其导航 Controller 。
在VideoCallViewController中
deinit {
print("Deinitializing VC)
}
在AppDelegate中
func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, forType type: PKPushType) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
videoVC = storyboard.instantiateViewController(withIdentifier: "VideoCallViewController") as! VideoCallViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = videoVC
self.window?.makeKeyAndVisible()
}
最佳答案
可能的解决方案:创建对可为空的窗口的单例保持引用,以负责视频通话流程。在通知上更改可见窗口。在通话结束时-返回到应用程序主窗口。
例如。 :
class VideoCallManager {
//MARK: - Singleton
static let sharedInstance = VideoCallManager()
private init() {}
private var videoCallWindow: UIWindow?
func navigateToVideoCallViewController() {
if let window = self.videoCallWindow, window.keyWindow {
//VideoCallViewController is displayed at the moment.
return
}
videoCallWindow = UIWindow.init(frame: UIScreen.mainScreen().bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
videoVC = storyboard.instantiateViewController(withIdentifier: "VideoCallViewController") as! VideoCallViewController
self.videoCallWindow?.rootViewController = videoVC
self.videoCallWindow?.makeKeyAndVisible()
}
func returnToWindowOfAppDelegate() {
if let window = self.videoCallWindow, window.keyWindow {
(UIApplication.sharedApplication().delegate as? AppDelegate)?.window?.makeKeyAndVisible()
self.videoCallWindow = nil
}
}
}
您的方法将如下所示:
func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, forType type: PKPushType) {
VideoCallManager.sharedInstance.navigateToVideoCallViewController()
}