Skip to content

Commit

Permalink
Merge pull request #46 from SongJiyeon/feature/present-view-on-keywindow
Browse files Browse the repository at this point in the history
🐛 Present view on keyWindow over iOS 17
  • Loading branch information
stleamist authored Oct 29, 2023
2 parents 9f997fb + 0ff5cb3 commit df7db51
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions Sources/BetterSafariView/SafariView/SafariViewPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit df7db51

Please sign in to comment.