DEV Community

ShoheOhtani
ShoheOhtani

Posted on

3 1

[Swift/SwiftUI] How to fix "Attempt to present which is already presenting"

When you implement present ViewController like Modal, some of case you'd see "Attempt to present which is already presenting".

Before code

uiView.window?.rootViewController?.present(viewController, animated: true)
Enter fullscreen mode Exit fullscreen mode

Error log

[Presentation] Attempt to present <UIViewController: 0x10c72d050> on <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x10c60ab30> (from <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x10c60ab30>) which is already presenting <UIViewController: 0x1201f72b0>.
Enter fullscreen mode Exit fullscreen mode

After code

guard let rootViewController = 
uiView.window?.rootViewController else { return }
if rootViewController.presentedViewController == nil {
    rootViewController.present(viewController, animated: true)
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay