Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid creation of multiple ViewController instances, iOS Swift

In AppDelegate, the below function gets called whenever VoIP call (push notification) is received thereby creating multiple instances of "VideoCallViewController"

I've used deinit (in VideoCallViewController) as shown below, to check if the previous instance of "VideoCallViewController" was being de-initialised before a new instance of "VideoCallViewController" is created, to my surprise print("Deinitializing VC) wasn't called, leaving the instance in memory.

How can I show VideoCallViewController with its Navigation Controller if an instance of VideoCallViewController already exists from AppDelegate.

In VideoCallViewController

deinit {
  print("Deinitializing VC)
}

In 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()

}
like image 218
SpaceX Avatar asked Oct 18 '22 05:10

SpaceX


2 Answers

You can create a lazy var in your AppDelegate

lazy var videoVC: UIViewController = {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    return storyboard.instantiateViewController(withIdentifier: "VideoCallViewController")
}()

Then in your pushRegistry:

func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, forType type: PKPushType) {

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

}
like image 168
Y.Bonafons Avatar answered Oct 21 '22 06:10

Y.Bonafons


Possible solution: Create singleton holding reference to nullable window, responsible for video calls flow. On notification change visible windows. On call end - return to the application main window.

E.G. :

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
        }
    }
}

And your method would look like:

func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, forType type: PKPushType) {
    VideoCallManager.sharedInstance.navigateToVideoCallViewController()
}
like image 41
dvp.petrov Avatar answered Oct 21 '22 07:10

dvp.petrov