I have a timer running in my UIApplication
subclass, that is should send the user to a certain ViewController
when it runs out.
I am able to instantiate the ViewController
I want to go to...
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "StartVC")
...but I do not know how to actually present it. Inside AppDelegate
I would be able to do window.rootViewController
etc. But this is not available in my UIApplication
subclass.
I have also tried to self.windows[0].rootViewController
but that is always just the first ViewController
, that was present when the app was started. Same with self.keyWindow.rootViewController
. And I honestly do not know what both of there properties are.
Full code for context:
import Foundation
import UIKit
class MyApplication: UIApplication {
var inactivityTimer: Timer!
override init() {
super.init()
restartInactivityTimer()
}
@objc func timerExceeded() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "StartVC")
//...here I would need to present "vc"
}
override func sendEvent(_ event: UIEvent) {
super.sendEvent(event)
restartInactivityTimer()
}
func restartInactivityTimer() {
if inactivityTimer != nil { inactivityTimer.invalidate() }
inactivityTimer = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(timerExceeded), userInfo: nil, repeats: false)
}
}