0

As the question says, I am trying to access a variable of my ViewController inside my AppDelegate.swift.

in func applicationWillTerminate(_ application: UIApplication){}, I want to access a WebSocket object inside my ViewController and do socket.disconnect(), so that the socket is disconnecting when my application is going to get shut downed.

I already searched the net for quite a bit but nothing seemed to help me in this situation.

  • how do you present the viewController that you want to access? are there any navigationControllers, or are they just presented? – Milan Nosáľ Jan 17 '18 at 20:10

1 Answers1

1

Try to add an observer in your view controller:

NotificationCenter.default.addObserver(self,
      selector: #selector(applicationWillTerminate),
      name: .UIApplicationWillTerminate,
      object: nil)

using callback:

@objc func applicationWillTerminate() {
    // Disconnect socket

}

Also I think everything will be disconnected when app terminates

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87