Skip to content
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

[ios]Fix compile error when conforming UIApplication to Launcher due to MainActor annotation #7100

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
4 changes: 4 additions & 0 deletions packages/url_launcher/url_launcher_ios/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 6.3.1

* Fixes a compile error when comforming UIApplication to Launcher in iOS 18 Beta 3.

## 6.3.0

* Adds Swift Package Manager compatibility.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,21 @@ protocol Launcher {
completionHandler completion: ((Bool) -> Void)?)
}

/// Launcher is intentionally a direct passthroguh to UIApplication.
extension UIApplication: Launcher {}
// TODO(hellohuanlin): This wrapper is a workaround for iOS 18 Beta 3 where completionHandler is annotated with @MainActor @Sendable, resulting in compile error when conforming UIApplication to Launcher. We should try again in newer betas.
/// A default URL launcher.
final class DefaultLauncher: Launcher {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hellohuanlin Launcher is documented to only exist for testing purposes.

/// Protocol for UIApplication methods relating to launching URLs.
///
/// This protocol exists to allow injecting an alternate implementation for testing.

I know you reviewed the PR, based on your DeviceProtocol example, that's the idiomatic way you suggest doing this, or is there another way?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idiomatic way is (as shown in wwdc's demo code a few years ago):

(1) for real dependency, extend system type to conform to our protocol (e.g. extension UIApplication: Launcher)
(2) for fake dependency, define a custom type that conforms to the same protocol (e.g. class MockLauncher: Launcher)

In this case, (1) doesn't work anymore. So we have to define the custom type (e.g. class DefaultLauncher: Launcher).

However, I highly suspect Apple may fix it in newer betas, so that we can revert this PR. As discussed in the PR description, Apple seems to suggest that if a module is not ready to adopt swift concurrency, it should just work as before. So they are not supposed to break us.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like ios18 and xcode update break it...

func canOpenURL(_ url: URL) -> Bool {
return UIApplication.shared.canOpenURL(url)
}

func open(
_ url: URL,
options: [UIApplication.OpenExternalURLOptionsKey: Any],
completionHandler completion: ((Bool) -> Void)?
) {
UIApplication.shared.open(
url,
options: options,
completionHandler: completion)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public final class URLLauncherPlugin: NSObject, FlutterPlugin, UrlLauncherApi {
UIApplication.shared.keyWindow?.rootViewController?.topViewController
}

init(launcher: Launcher = UIApplication.shared) {
init(launcher: Launcher = DefaultLauncher()) {
self.launcher = launcher
}

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.0
version: 6.3.1

environment:
sdk: ^3.2.3
Expand Down