SwiftMessages
SwiftMessages copied to clipboard
SwiftMessages prevents reset root view controller
Issue
I have a function that logout my user:
func logout() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateInitialViewController()
UIApplication.shared.keyWindow?.rootViewController = controller
UIApplication.shared.keyWindow?.makeKeyAndVisible()
}
but it doesn't work when a notification is displayed:
type: .warning
layout: .statusLine
duration: .forever
Confirguration
Version: iOS 11 Device: iPhone 6s SDK: Swift 3 SwiftMessages Version: 4.0.0
Have you an idea ?
Hi.
I would think you'd want to set the root view controller on UIApplication.shared.delegate?.window??
not UIApplication.shared.keyWindow?
which isn't necessarily your application's main window.
If that isn't helpful, a few questions:
- What happens?
- What is the
presentationContext
setting? - Are you setting the
SwiftMessages.Config.becomeKeyWindow
property?
While debugging with the displayed notification
On iOS 10
po UIApplication.shared.keyWindow?.rootViewController
returns:
▿ Optional<UIViewController>
▿ some : <Target.HomeViewController: 0x10238c410>
On iOS 11
po UIApplication.shared.keyWindow?.rootViewController
returns:
▿ Optional<UIViewController>
▿ some : <SwiftMessages.WindowViewController: 0x10c3005e0>
I found a trick to fix my issue:
func logout() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateInitialViewController()
if !(UIApplication.shared.keyWindow?.rootViewController is HomeViewController) {
UIApplication.shared.keyWindow?.isHidden = true
}
UIApplication.shared.keyWindow?.rootViewController = controller
UIApplication.shared.keyWindow?.makeKeyAndVisible()
}
Otherwise, I tried with UIApplication.shared.delegate?.window??
and it works fine !
Thanks. I'll need to investigate why the key window behavior changed in iOS 11.
I haven't been able to reproduce this. Try running the attached test app. Tap "Show" and then "Hide" and check the key window instance IDs printed in the console. For me they match. I'm not sure what else I can do unless you can help me reproduce.