I need to call a method in my ViewController class in AppDelegate. This is what the method looks like this:
class ViewController: UIViewController {
func myFunction() {
...
}
}
I've tried using window.rootViewController, but it's telling me the method myFunction does not exist, since rootViewController is of type UIViewController.
I've also tried
var vc = ViewController()
self.vc.myFunction()
But this is creating a new instance of the ViewController class, and that's not what I want, either.
Here is the part of the AppDelegate code that I need myFunction to be called from:
class AppDelegate: UIResponder, UIApplicationDelegate {
func handleEvent() {
if UIApplication.shared.applicationState == .active {
// call myFunction()
}
else {
...
}
}
}
Context: I'm following a geofencing tutorial: https://www.raywenderlich.com/136165/core-location-geofencing-tutorial
Since user location is being monitored even when app is closed, when an event is triggered, AppDelegate should be used to handle the event, since view controller is not yet loaded.
In my AppDelegate that handles event, if the application is active, I would like to pop up an alert to have the user choose to stop monitoring location. The function that stops location monitoring (myFunction) is in view controller.