-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[url_launcher] migrating objc plugin to swift #4753
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
Changes from 1 commit
1d5609f
d5727dd
c4e55fb
050d6b3
5278199
55a345b
8356d7a
120b2d4
1316f3c
7bd3553
2741de4
495de26
c898698
6a6c8d5
254b9af
869f18f
1120baa
9f591e3
13cd002
f8f5f0b
827204a
30568fd
6b5e4cd
3651a97
257b7cc
3ee9d96
d90702f
7241d20
9d578f2
657c011
7a2fac5
4d4f1bb
7b6272f
b91dca7
e7e011a
d955ef8
1297683
43966a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,34 +20,28 @@ final class URLLauncherTests: XCTestCase { | |
|
|
||
| func testCanLaunchSuccess() { | ||
| let result = createPlugin().canLaunchUrl(url: "good://url") | ||
| XCTAssertEqual(result.result, .success) | ||
| XCTAssertEqual(result, .success) | ||
| } | ||
|
|
||
| func testCanLaunchFailure() { | ||
| let result = createPlugin().canLaunchUrl(url: "bad://url") | ||
| XCTAssertEqual(result.result, .failure) | ||
| XCTAssertEqual(result, .failedToLoad) | ||
| } | ||
|
|
||
| func testCanLaunchFailureWithInvalidURL() { | ||
| let result = createPlugin().canLaunchUrl(url: "urls can't have spaces") | ||
| if result.result == .failure { | ||
| // When linking against the iOS 17 SDK or later, NSURL uses a lenient parser, and won't | ||
| // fail to parse URLs, so the test must allow for either outcome. | ||
| XCTAssertNil(result.errorMessage) | ||
| XCTAssertNil(result.errorDetails) | ||
| } else { | ||
| XCTAssertEqual(result.result, .invalidUrl) | ||
| XCTAssertEqual(result.errorMessage, "Unable to parse URL") | ||
| XCTAssertEqual(result.errorDetails, "Provided URL: urls can't have spaces") | ||
| } | ||
|
|
||
| // When linking against the iOS 17 SDK or later, NSURL uses a lenient parser, and won't | ||
| // fail to parse URLs, so the test must allow for either outcome. | ||
| XCTAssertTrue(result == .failedToLoad || result == .invalidUrl) | ||
| } | ||
|
|
||
| func testLaunchSuccess() { | ||
| let expectation = XCTestExpectation(description: "completion called") | ||
| createPlugin().launchUrl(url: "good://url", universalLinksOnly: false) { result in | ||
| switch result { | ||
| case .success(let details): | ||
| XCTAssertEqual(details.result, .success) | ||
| XCTAssertEqual(details, .success) | ||
| case .failure(let error): | ||
| XCTFail("Unexpected error: \(error)") | ||
| } | ||
|
|
@@ -62,7 +56,7 @@ final class URLLauncherTests: XCTestCase { | |
| createPlugin().launchUrl(url: "bad://url", universalLinksOnly: false) { result in | ||
| switch result { | ||
| case .success(let details): | ||
| XCTAssertEqual(details.result, .failure) | ||
| XCTAssertEqual(details, .failedToLoad) | ||
| case .failure(let error): | ||
| XCTFail("Unexpected error: \(error)") | ||
| } | ||
|
|
@@ -77,16 +71,9 @@ final class URLLauncherTests: XCTestCase { | |
| createPlugin().launchUrl(url: "urls can't have spaces", universalLinksOnly: false) { result in | ||
| switch result { | ||
| case .success(let details): | ||
| if details.result == .failure { | ||
| // When linking against the iOS 17 SDK or later, NSURL uses a lenient parser, and won't | ||
| // fail to parse URLs, so the test must allow for either outcome. | ||
| XCTAssertNil(details.errorMessage) | ||
| XCTAssertNil(details.errorDetails) | ||
| } else { | ||
| XCTAssertEqual(details.result, .invalidUrl) | ||
| XCTAssertEqual(details.errorMessage, "Unable to parse URL") | ||
| XCTAssertEqual(details.errorDetails, "Provided URL: urls can't have spaces") | ||
| } | ||
| // When linking against the iOS 17 SDK or later, NSURL uses a lenient parser, and won't | ||
| // fail to parse URLs, so the test must allow for either outcome. | ||
| XCTAssertTrue(details == .failedToLoad || details == .invalidUrl) | ||
|
||
| case .failure(let error): | ||
| XCTFail("Unexpected error: \(error)") | ||
| } | ||
|
|
@@ -104,7 +91,7 @@ final class URLLauncherTests: XCTestCase { | |
| plugin.launchUrl(url: "good://url", universalLinksOnly: false) { result in | ||
| switch result { | ||
| case .success(let details): | ||
| XCTAssertEqual(details.result, .success) | ||
| XCTAssertEqual(details, .success) | ||
| case .failure(let error): | ||
| XCTFail("Unexpected error: \(error)") | ||
| } | ||
|
|
@@ -123,7 +110,7 @@ final class URLLauncherTests: XCTestCase { | |
| plugin.launchUrl(url: "good://url", universalLinksOnly: true) { result in | ||
| switch result { | ||
| case .success(let details): | ||
| XCTAssertEqual(details.result, .success) | ||
| XCTAssertEqual(details, .success) | ||
| case .failure(let error): | ||
| XCTFail("Unexpected error: \(error)") | ||
| } | ||
|
|
@@ -144,7 +131,8 @@ final private class FakeLauncher: NSObject, Launcher { | |
| } | ||
|
|
||
| func openURL( | ||
| _ url: URL, options: [UIApplication.OpenExternalURLOptionsKey: Any], | ||
| _ url: URL, | ||
| options: [UIApplication.OpenExternalURLOptionsKey: Any], | ||
| completionHandler completion: ((Bool) -> Void)? | ||
hellohuanlin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) { | ||
| self.passedOptions = options | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,7 @@ public final class URLLauncherPlugin: NSObject, FlutterPlugin, UrlLauncherApi { | |
| private var currentSession: URLLaunchSession? | ||
| private let launcher: Launcher | ||
|
|
||
| var topViewController: UIViewController? { | ||
| private var topViewController: UIViewController? { | ||
| // TODO(stuartmorgan) Provide a non-deprecated codepath. See | ||
| // https://github.com/flutter/flutter/issues/104117 | ||
| UIApplication.shared.keyWindow?.rootViewController?.topViewController | ||
|
|
@@ -25,34 +25,33 @@ public final class URLLauncherPlugin: NSObject, FlutterPlugin, UrlLauncherApi { | |
| self.launcher = launcher | ||
| } | ||
|
|
||
| func canLaunchUrl(url: String) -> LaunchResultDetails { | ||
| func canLaunchUrl(url: String) -> LaunchResult { | ||
| guard let url = URL(string: url) else { | ||
| return invalidURLError(for: url) | ||
| return .failedToLoad | ||
| } | ||
| let canOpen = launcher.canOpenURL(url) | ||
| return LaunchResultDetails(result: canOpen ? .success : .failure) | ||
| return canOpen ? .success : .failedToLoad | ||
| } | ||
|
|
||
| func launchUrl( | ||
| url: String, universalLinksOnly: Bool, | ||
|
||
| completion: @escaping (Result<LaunchResultDetails, Error>) -> Void | ||
| completion: @escaping (Result<LaunchResult, Error>) -> Void | ||
| ) { | ||
| guard let url = URL(string: url) else { | ||
| completion(.success(invalidURLError(for: url))) | ||
| completion(.success(.failedToLoad)) | ||
| return | ||
| } | ||
| let options = [UIApplication.OpenExternalURLOptionsKey.universalLinksOnly: universalLinksOnly] | ||
| launcher.openURL(url, options: options) { success in | ||
| let result = LaunchResultDetails(result: success ? .success : .failure) | ||
| completion(.success(result)) | ||
| launcher.openURL(url, options: options) { result in | ||
| completion(.success(result ? .success : .failedToLoad)) | ||
| } | ||
| } | ||
|
|
||
| func openUrlInSafariViewController( | ||
| url: String, completion: @escaping (Result<LaunchResultDetails, Error>) -> Void | ||
| url: String, completion: @escaping (Result<LaunchResult, Error>) -> Void | ||
|
||
| ) { | ||
| guard let url = URL(string: url) else { | ||
| completion(.success(invalidURLError(for: url))) | ||
| completion(.success(.failedToLoad)) | ||
| return | ||
| } | ||
|
|
||
|
|
@@ -68,18 +67,6 @@ public final class URLLauncherPlugin: NSObject, FlutterPlugin, UrlLauncherApi { | |
| func closeSafariViewController() throws { | ||
|
||
| currentSession?.close() | ||
| } | ||
|
|
||
| /** | ||
| * Creates an error for an invalid URL string. | ||
| * | ||
| * @param url The invalid URL string | ||
| * @return The error to return | ||
| */ | ||
| func invalidURLError(for url: String) -> LaunchResultDetails { | ||
| LaunchResultDetails( | ||
| result: .invalidUrl, errorMessage: "Unable to parse URL", errorDetails: "Provided URL: \(url)" | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| /// This method recursively iterates through the view hierarchy | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.