Skip to content

Commit

Permalink
[url_launcher] Fix launch mode types in new APIs (#5232)
Browse files Browse the repository at this point in the history
The new query APIs added in 6.2.0 accidentally used `PreferredLaunchMode`, which is only intended for the platform interface layer, rather than `LaunchMode`, which is the app-facing analog (they are separate to allow controlling when each is updated separately).

Technically this is a breaking change, but 6.2.0 was out for less than 24 hours, so extremely few people will be affected (they would have to have updated, written code against the new API, and worked around the lack of type export), and a breaking change to fix this would have much more disruptive effects on the ecosystem.

Fixes flutter/flutter#137278
  • Loading branch information
stuartmorgan authored Oct 25, 2023
1 parent f2124f7 commit fea24c5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
7 changes: 7 additions & 0 deletions packages/url_launcher/url_launcher/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## 6.2.1

* Fixes incorrect types in `supportsLaunchMode` and
`supportsCloseForLaunchMode`.

## 6.2.0

_Retracted due to incorrect types in new APIs._

* Adds `supportsLaunchMode` for checking whether the current platform supports a
given launch mode, to allow clients that will only work with specific modes
to avoid fallback to a different mode.
Expand Down
6 changes: 3 additions & 3 deletions packages/url_launcher/url_launcher/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ See [`-[UIApplication canOpenURL:]`](https://developer.apple.com/documentation/u
Add any URL schemes passed to `canLaunchUrl` as `<queries>` entries in your
`AndroidManifest.xml`, otherwise it will return false in most cases starting
on Android 11 (API 30) or higher. Checking for
`supportsLaunchMode(PreferredLaunchMode.inAppBrowserView)` also requires
`supportsLaunchMode(LaunchMode.inAppBrowserView)` also requires
a `<queries>` entry to return anything but false. A `<queries>`
element must be added to your manifest as a child of the root element.

Expand Down Expand Up @@ -222,9 +222,9 @@ On some platforms, web URLs can be launched either in an in-app web view, or
in the default browser. The default behavior depends on the platform (see
[`launchUrl`](https://pub.dev/documentation/url_launcher/latest/url_launcher/launchUrl.html)
for details), but a specific mode can be used on supported platforms by
passing a `PreferredLaunchMode`.
passing a `LaunchMode`.

Platforms that do no support a requested `PreferredLaunchMode` will
Platforms that do no support a requested `LaunchMode` will
automatically fall back to a supported mode (usually `platformDefault`). If
your application needs to avoid that fallback behavior, however, you can check
if the current platform supports a given mode with `supportsLaunchMode` before
Expand Down
14 changes: 9 additions & 5 deletions packages/url_launcher/url_launcher/lib/src/url_launcher_uri.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

import 'dart:async';

import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
// PreferredLaunchMode is hidden to prevent accidentally using it in APIs at
// this layer. If it is ever needed in this file, it should be imported
// separately with a prefix.
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart'
hide PreferredLaunchMode;

import '../url_launcher_string.dart';
import 'type_conversion.dart';
Expand Down Expand Up @@ -81,15 +85,15 @@ Future<void> closeInAppWebView() async {
/// Calling [launchUrl] with an unsupported mode will fall back to a supported
/// mode, so calling this method is only necessary for cases where the caller
/// needs to know which mode will be used.
Future<bool> supportsLaunchMode(PreferredLaunchMode mode) {
return UrlLauncherPlatform.instance.supportsMode(mode);
Future<bool> supportsLaunchMode(LaunchMode mode) {
return UrlLauncherPlatform.instance.supportsMode(convertLaunchMode(mode));
}

/// Returns true if [closeInAppWebView] is supported for [mode] in the current
/// platform implementation.
///
/// If this returns false, [closeInAppWebView] will not work when launching
/// URLs with [mode].
Future<bool> supportsCloseForLaunchMode(PreferredLaunchMode mode) {
return UrlLauncherPlatform.instance.supportsMode(mode);
Future<bool> supportsCloseForLaunchMode(LaunchMode mode) {
return UrlLauncherPlatform.instance.supportsMode(convertLaunchMode(mode));
}
2 changes: 1 addition & 1 deletion packages/url_launcher/url_launcher/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for launching a URL. Supports
web, phone, SMS, and email schemes.
repository: https://github.com/flutter/packages/tree/main/packages/url_launcher/url_launcher
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+url_launcher%22
version: 6.2.0
version: 6.2.1

environment:
sdk: ">=3.1.0 <4.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,37 +251,35 @@ void main() {

group('supportsLaunchMode', () {
test('handles returning true', () async {
const PreferredLaunchMode mode = PreferredLaunchMode.inAppBrowserView;
mock.setResponse(true);

expect(await supportsLaunchMode(mode), true);
expect(mock.launchMode, mode);
expect(await supportsLaunchMode(LaunchMode.inAppBrowserView), true);
expect(mock.launchMode, PreferredLaunchMode.inAppBrowserView);
});

test('handles returning false', () async {
const PreferredLaunchMode mode = PreferredLaunchMode.inAppBrowserView;
mock.setResponse(false);

expect(await supportsLaunchMode(mode), false);
expect(mock.launchMode, mode);
expect(await supportsLaunchMode(LaunchMode.inAppBrowserView), false);
expect(mock.launchMode, PreferredLaunchMode.inAppBrowserView);
});
});

group('supportsCloseForLaunchMode', () {
test('handles returning true', () async {
const PreferredLaunchMode mode = PreferredLaunchMode.inAppBrowserView;
mock.setResponse(true);

expect(await supportsCloseForLaunchMode(mode), true);
expect(mock.launchMode, mode);
expect(
await supportsCloseForLaunchMode(LaunchMode.inAppBrowserView), true);
expect(mock.launchMode, PreferredLaunchMode.inAppBrowserView);
});

test('handles returning false', () async {
const PreferredLaunchMode mode = PreferredLaunchMode.inAppBrowserView;
mock.setResponse(false);

expect(await supportsCloseForLaunchMode(mode), false);
expect(mock.launchMode, mode);
expect(
await supportsCloseForLaunchMode(LaunchMode.inAppBrowserView), false);
expect(mock.launchMode, PreferredLaunchMode.inAppBrowserView);
});
});
}

0 comments on commit fea24c5

Please sign in to comment.