SwiftKickMobile / SwiftMessages

A very flexible message bar for UIKit and SwiftUI.
MIT License
7.32k stars 746 forks source link

SwiftMessages prevents reset root view controller #132

Open grifas opened 6 years ago

grifas commented 6 years ago

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 ?

wtmoose commented 6 years ago

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:

  1. What happens?
  2. What is the presentationContext setting?
  3. Are you setting the SwiftMessages.Config.becomeKeyWindow property?
grifas commented 6 years ago

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 !

wtmoose commented 6 years ago

Thanks. I'll need to investigate why the key window behavior changed in iOS 11.

wtmoose commented 6 years ago

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.

SwiftMessagesTest.zip