Skip to content

[url_lancher] Don't use canLaunchUrl in Link #4400

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

Merged
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
3 changes: 2 additions & 1 deletion packages/url_launcher/url_launcher/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 6.1.12

* Removes the use of `canLaunchUrl` in `Link`, to avoid issues on platforms where `canLaunchUrl` is unreliable or requires permissions.
* Updates minimum supported macOS version to 10.14.
* Fixes stale ignore: prefer_const_constructors.
* Updates minimum supported SDK version to Flutter 3.10/Dart 3.0.
Expand Down
4 changes: 2 additions & 2 deletions packages/url_launcher/url_launcher/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ To use this plugin, add `url_launcher` as a [dependency in your pubspec.yaml fil
### Example

<?code-excerpt "basic.dart (basic-example)"?>
``` dart
```dart
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

Expand Down Expand Up @@ -72,7 +72,7 @@ element must be added to your manifest as a child of the root element.
Example:

<?code-excerpt "../../android/app/src/main/AndroidManifest.xml (android-queries)" plaster="none"?>
``` xml
```xml
<!-- Provide required visibility configuration for API level 30 and above -->
<queries>
<!-- If your app checks for SMS support -->
Expand Down
10 changes: 7 additions & 3 deletions packages/url_launcher/url_launcher/lib/src/link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,18 @@ class DefaultLinkDelegate extends StatelessWidget {

// At this point, we know that the link is external. So we use the
// `launchUrl` API to open the link.
if (await canLaunchUrl(url)) {
await launchUrl(
bool success;
try {
success = await launchUrl(
url,
mode: _useWebView
? LaunchMode.inAppWebView
: LaunchMode.externalApplication,
);
} else {
} on PlatformException {
success = false;
}
if (!success) {
FlutterError.reportError(FlutterErrorDetails(
exception: 'Could not launch link $url',
stack: StackTrace.current,
Expand Down
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.1.11
version: 6.1.12

environment:
sdk: ">=3.0.0 <4.0.0"
Expand Down
8 changes: 6 additions & 2 deletions packages/url_launcher/url_launcher/test/link_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ void main() {
)
..setResponse(true);
await followLink!();
expect(mock.canLaunchCalled, isTrue);
// Calling canLaunch just to pre-check launch is an anti-pattern, since
// canLaunch doesn't always work, so ensure that it's not called.
expect(mock.canLaunchCalled, isFalse);
expect(mock.launchCalled, isTrue);
});

Expand Down Expand Up @@ -93,7 +95,9 @@ void main() {
)
..setResponse(true);
await followLink!();
expect(mock.canLaunchCalled, isTrue);
// Calling canLaunch just to pre-check launch is an anti-pattern, since
// canLaunch doesn't always work, so ensure that it's not called.
expect(mock.canLaunchCalled, isFalse);
expect(mock.launchCalled, isTrue);
});

Expand Down