DEV Community

ShoheOhtani
ShoheOhtani

Posted on

[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)