Skip to content

[url_launcher] When not fully loaded, clicking close causes the callback to not be triggered correctly. #8582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/url_launcher/url_launcher_ios/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## NEXT
## 6.3.3

* Updates minimum supported SDK version to Flutter 3.22/Dart 3.4.
* Ensures the completion callback is invoked if the user dismisses the Safari view before the initial URL load completes.

## 6.3.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,24 @@ final class URLLauncherTests: XCTestCase {
XCTAssertEqual(launcher.passedOptions?[.universalLinksOnly] as? Bool, true)
}

func testLaunchSafariViewControllerWithClose() {
let launcher = FakeLauncher()
let plugin = createPlugin(launcher: launcher)

let expectation = XCTestExpectation(description: "completion called")
plugin.openUrlInSafariViewController(url: "https://flutter.dev") { result in
switch result {
case .success(let details):
XCTAssertEqual(details, .dismissed)
case .failure(let error):
XCTFail("Unexpected error: \(error)")
}
expectation.fulfill()
}
plugin.closeSafariViewController()
wait(for: [expectation], timeout: 30)
}

}

final private class FakeLauncher: NSObject, Launcher {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ final class URLLaunchSession: NSObject, SFSafariViewControllerDelegate {

private let completion: OpenInSafariCompletionHandler
private let url: URL
private var isLoadCompleted: Bool = false

/// The Safari view controller used for displaying the URL.
let safariViewController: SFSafariViewController
Expand Down Expand Up @@ -46,12 +47,16 @@ final class URLLaunchSession: NSObject, SFSafariViewControllerDelegate {
} else {
completion(.success(.failedToLoad))
}
isLoadCompleted = true
}

/// Called when the user finishes using the Safari view controller.
///
/// - Parameter controller: The Safari view controller.
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
if !isLoadCompleted {
completion(.success(.dismissed))
}
controller.dismiss(animated: true, completion: nil)
didFinish?()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ enum InAppLoadResult: Int {
case failedToLoad = 1
/// The URL could not be launched because it is invalid.
case invalidUrl = 2
/// The controller was closed before loading.
case dismissed = 3
}

private class messagesPigeonCodecReader: FlutterStandardReader {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ enum InAppLoadResult {

/// The URL could not be launched because it is invalid.
invalidUrl,

/// The controller was closed before loading.
dismissed,
}

class _PigeonCodec extends StandardMessageCodec {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ class UrlLauncherIOS extends UrlLauncherPlatform {
throw _failedSafariViewControllerLoadException(url);
case InAppLoadResult.invalidUrl:
throw _invalidUrlException();
case InAppLoadResult.dismissed:
return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ enum InAppLoadResult {

/// The URL could not be launched because it is invalid.
invalidUrl,

/// The controller was closed before loading.
dismissed,
}

@HostApi()
Expand Down
2 changes: 1 addition & 1 deletion packages/url_launcher/url_launcher_ios/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: url_launcher_ios
description: iOS implementation of the url_launcher plugin.
repository: https://github.com/flutter/packages/tree/main/packages/url_launcher/url_launcher_ios
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+url_launcher%22
version: 6.3.2
version: 6.3.3

environment:
sdk: ^3.4.0
Expand Down