From 0ff5cb35f6a20836f791192159429d3dc914043f Mon Sep 17 00:00:00 2001 From: Songjiyeon Date: Tue, 24 Oct 2023 17:49:00 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Present=20view=20on=20keyWindow?= =?UTF-8?q?=20over=20iOS=2017?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SafariView/SafariViewPresenter.swift | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/Sources/BetterSafariView/SafariView/SafariViewPresenter.swift b/Sources/BetterSafariView/SafariView/SafariViewPresenter.swift index ec98958..255529c 100644 --- a/Sources/BetterSafariView/SafariView/SafariViewPresenter.swift +++ b/Sources/BetterSafariView/SafariView/SafariViewPresenter.swift @@ -79,20 +79,31 @@ extension SafariViewPresenter { let safariViewController = SFSafariViewController(url: representation.url, configuration: representation.configuration) safariViewController.delegate = self representation.applyModification(to: safariViewController) - - // Present a Safari view controller from the `viewController` of `UIViewRepresentable`, instead of `UIViewControllerRepresentable`. - // This fixes an issue where the Safari view controller is not presented properly - // when the `UIViewControllerRepresentable` is detached from the root view controller (e.g. `UIViewController` contained in `UITableViewCell`) - // while allowing it to be presented even on the modal sheets. - // Thanks to: Bohdan Hernandez Navia (@boherna) - guard let presentingViewController = uiView.viewController else { - self.resetItemBinding() + + if #available(iOS 17.0, *) { + if var topController = UIApplication.shared.windows.filter(\.isKeyWindow).first?.rootViewController { + while let presentedViewController = topController.presentedViewController { + topController = presentedViewController + } + topController.present(safariViewController, animated: true, completion: nil) + } + self.safariViewController = safariViewController return + } else { + // Present a Safari view controller from the `viewController` of `UIViewRepresentable`, instead of `UIViewControllerRepresentable`. + // This fixes an issue where the Safari view controller is not presented properly + // when the `UIViewControllerRepresentable` is detached from the root view controller (e.g. `UIViewController` contained in `UITableViewCell`) + // while allowing it to be presented even on the modal sheets. + // Thanks to: Bohdan Hernandez Navia (@boherna) + guard let presentingViewController = uiView.viewController else { + self.resetItemBinding() + return + } + + presentingViewController.present(safariViewController, animated: true) + + self.safariViewController = safariViewController } - - presentingViewController.present(safariViewController, animated: true) - - self.safariViewController = safariViewController } private func updateSafariViewController(with item: Item) {