Skip to content

Commit

Permalink
Bump to v2.4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
stleamist committed Oct 29, 2023
2 parents dffd42b + 71810a6 commit bcb2313
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 32 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [v2.4.2](https://github.com/stleamist/BetterSafariView/releases/tag/v2.4.2) (2023-10-30)
### Fixed
- Fixed an issue where the `SafariViewPresenter` fails to find a view controller to presented from (#41, #46). Thanks, @Tunous and @SongJiyeon!

## [v2.4.1](https://github.com/stleamist/BetterSafariView/releases/tag/v2.4.1) (2023-01-15)
### Fixed
- Fixed an issue where the `WebAuthenticationPresenter` fails to find its presentation anchor (#22). Thanks, @kevvdevv, @exentrich, and @ldstreet!
Expand Down
2 changes: 1 addition & 1 deletion Demo/iOS/Views/RootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ struct RootView: View {
WebAuthenticationSessionOptionsForm(options: $webAuthenticationSessionOptions)
}
}

Section(header: Text("NaiveSafariView" + "\n" + "(Just for comparison. Do not use in practice.)").textCase(nil)) {
Button(action: { showingNaiveSafariViewSheet = true }) {
HStack {
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func prefersEphemeralWebBrowserSession(_ prefersEphemeralWebBrowserSession: Bool
Add the following line to the `dependencies` in your [`Package.swift`](https://developer.apple.com/documentation/swift_packages/package) file:

```swift
.package(url: "https://github.com/stleamist/BetterSafariView.git", .upToNextMajor(from: "2.4.1"))
.package(url: "https://github.com/stleamist/BetterSafariView.git", .upToNextMajor(from: "2.4.2"))
```

Next, add `BetterSafariView` as a dependency for your targets:
Expand All @@ -304,7 +304,7 @@ import PackageDescription
let package = Package(
name: "MyPackage",
dependencies: [
.package(url: "https://github.com/stleamist/BetterSafariView.git", .upToNextMajor(from: "2.4.1"))
.package(url: "https://github.com/stleamist/BetterSafariView.git", .upToNextMajor(from: "2.4.2"))
],
targets: [
.target(name: "MyTarget", dependencies: ["BetterSafariView"])
Expand Down
19 changes: 12 additions & 7 deletions Sources/BetterSafariView/SafariView/SafariViewPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import SwiftUI
import SafariServices

// `SafariViewPresenter` conforms `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`).
struct SafariViewPresenter<Item: Identifiable>: UIViewRepresentable {

// MARK: Representation
Expand Down Expand Up @@ -79,13 +83,14 @@ 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 {

// Presents a Safari view controller from the farthest `presentedViewController` of `UIWindow`.
// (same approach when presenting `UIAlertController`)
guard let presentingViewController = uiView.window?.farthestPresentedViewController else {
assertionFailure(
"Cannot find the view controller to present from."
+ " This happens when a 'SafariViewPresenter' is detached from the window, or the window doesn't have 'rootViewController.'"
)
self.resetItemBinding()
return
}
Expand Down
21 changes: 0 additions & 21 deletions Sources/BetterSafariView/Shared/UIView+viewController.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#if os(iOS)

import UIKit

extension UIWindow {

/// The view controller that was presented modally on top of the window.
var farthestPresentedViewController: UIViewController? {
guard let rootViewController = rootViewController else { return nil }
return Array(sequence(first: rootViewController, next: \.presentedViewController)).last
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ extension WebAuthenticationPresenter {

@available(iOS, introduced: 13.0, deprecated: 14.0)
func setInteractiveDismissalDelegateIfPossible() {
guard let safariViewController = view.viewController?.presentedViewController as? SFSafariViewController else {
guard let safariViewController = view.window?.farthestPresentedViewController as? SFSafariViewController else {
return
}
safariViewController.presentationController?.delegate = interactiveDismissalDelegate
Expand Down

0 comments on commit bcb2313

Please sign in to comment.