SwiftMessages icon indicating copy to clipboard operation
SwiftMessages copied to clipboard

SwiftMessages prevents reset root view controller

Open grifas opened this issue 6 years ago • 4 comments

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 ?

grifas avatar Oct 12 '17 13:10 grifas

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?

wtmoose avatar Oct 12 '17 14:10 wtmoose

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 !

grifas avatar Oct 12 '17 15:10 grifas

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

wtmoose avatar Oct 12 '17 15:10 wtmoose

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

wtmoose avatar Nov 10 '17 14:11 wtmoose