Skip to content

Commit 33caf1d

Browse files
authored
[ios]Fix compile error when conforming UIApplication to Launcher due to MainActor annotation (#7100)
iOS 18 Beta 3 added `@MainActor @Sendable` for openURL's completion handler: ``` @available(iOS 10.0, *) open func open(_ url: URL, options: [UIApplication.OpenExternalURLOptionsKey : Any] = [:], completionHandler completion: (@mainactor @sendable (Bool) -> Void)? = nil) ``` The addition of `@MainActor` is the one that caused the compile error. It was not there for Beta 1. This PR we simply use a `DefaultLauncher` wrapper. As expected, passing in a **non-isolated closure** to a function that expects a **main-actor isolated closure** is fine, since non-isolated closure can be called in any isolation context; plus the minimal concurrency checking also silences the Sendable warning. But we should check again in newer betas if we can revert this change. Because this compile error forces some libraries to update for swift concurrency, which seems to be against the whole idea of having 3 different levels of concurrency checking. So I suspect that Apple is still hashing things out and may change it back (either by removing `@MainActor` from signature, or compiler change to allow this conformance under minimal checking) *List which issues are fixed by this PR. You must list at least one issue.* Fixes flutter/flutter#151467
1 parent 21e3340 commit 33caf1d

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

packages/url_launcher/url_launcher_ios/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 6.3.1
2+
3+
* Fixes a compile error when comforming UIApplication to Launcher in iOS 18 Beta 3.
4+
15
## 6.3.0
26

37
* Adds Swift Package Manager compatibility.

packages/url_launcher/url_launcher_ios/ios/url_launcher_ios/Sources/url_launcher_ios/Launcher.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,21 @@ protocol Launcher {
1818
completionHandler completion: ((Bool) -> Void)?)
1919
}
2020

21-
/// Launcher is intentionally a direct passthroguh to UIApplication.
22-
extension UIApplication: Launcher {}
21+
// 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.
22+
/// A default URL launcher.
23+
final class DefaultLauncher: Launcher {
24+
func canOpenURL(_ url: URL) -> Bool {
25+
return UIApplication.shared.canOpenURL(url)
26+
}
27+
28+
func open(
29+
_ url: URL,
30+
options: [UIApplication.OpenExternalURLOptionsKey: Any],
31+
completionHandler completion: ((Bool) -> Void)?
32+
) {
33+
UIApplication.shared.open(
34+
url,
35+
options: options,
36+
completionHandler: completion)
37+
}
38+
}

packages/url_launcher/url_launcher_ios/ios/url_launcher_ios/Sources/url_launcher_ios/URLLauncherPlugin.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public final class URLLauncherPlugin: NSObject, FlutterPlugin, UrlLauncherApi {
2222
UIApplication.shared.keyWindow?.rootViewController?.topViewController
2323
}
2424

25-
init(launcher: Launcher = UIApplication.shared) {
25+
init(launcher: Launcher = DefaultLauncher()) {
2626
self.launcher = launcher
2727
}
2828

packages/url_launcher/url_launcher_ios/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: url_launcher_ios
22
description: iOS implementation of the url_launcher plugin.
33
repository: https://github.com/flutter/packages/tree/main/packages/url_launcher/url_launcher_ios
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+url_launcher%22
5-
version: 6.3.0
5+
version: 6.3.1
66

77
environment:
88
sdk: ^3.2.3

0 commit comments

Comments
 (0)