Skip to content

Commit

Permalink
[ios]Fix compile error when conforming UIApplication to Launcher due …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
hellohuanlin committed Jul 12, 2024
1 parent 21e3340 commit 33caf1d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
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 {
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

0 comments on commit 33caf1d

Please sign in to comment.