Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[url_launcher] Add a workaround for Uri encoding #3817

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

* Update the README to describe a workaround to the `Uri` query
encoding bug.

## 6.0.6

* Require `url_launcher_platform_interface` 2.0.3. This fixes an issue
Expand Down
36 changes: 23 additions & 13 deletions packages/url_launcher/url_launcher/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ To use this plugin, add `url_launcher` as a [dependency in your pubspec.yaml fil

## Installation

### iOS
### iOS
Add any URL schemes passed to `canLaunch` as `LSApplicationQueriesSchemes` entries in your Info.plist file.

Example:
Example:
```
<key>LSApplicationQueriesSchemes</key>
<array>
Expand Down Expand Up @@ -73,25 +73,35 @@ apps installed, so can't open `tel:` or `mailto:` links.

### Encoding URLs

URLs must be properly encoded, especially when including spaces or other special characters. This can be done using the [`Uri` class](https://api.dart.dev/stable/2.7.1/dart-core/Uri-class.html):
URLs must be properly encoded, especially when including spaces or other special
characters. This can be done using the
[`Uri` class](https://api.dart.dev/stable/2.7.1/dart-core/Uri-class.html).
For example:
```dart
import 'dart:core';
import 'package:url_launcher/url_launcher.dart';
String? encodeQueryParameters(Map<String, String> params) {
return params.entries
.map((e) => '${Uri.encodeComponent(e.key)}=${Uri.encodeComponent(e.value)}')
.join('&');
}

final Uri _emailLaunchUri = Uri(
final Uri emailLaunchUri = Uri(
scheme: 'mailto',
path: '[email protected]',
queryParameters: {
query: encodeQueryParameters(<String, String>{
'subject': 'Example Subject & Symbols are allowed!'
}
}),
);

// ...

// mailto:[email protected]?subject=Example+Subject+%26+Symbols+are+allowed%21
launch(_emailLaunchUri.toString());
launch(emailLaunchUri.toString());
```

**Warning**: For any scheme other than `http` or `https`, you should use the
`query` parameter and the `encodeQueryParameters` function shown above rather
than `Uri`'s `queryParameters` constructor argument, due to
[a bug](https://github.com/dart-lang/sdk/issues/43838) in the way `Uri`
encodes query parameters. Using `queryParameters` will result in spaces being
converted to `+` in many cases.

## Handling missing URL receivers

A particular mobile device may not be able to receive all supported URL schemes.
Expand All @@ -113,4 +123,4 @@ By default, Android opens up a browser when handling URLs. You can pass
If you do this for a URL of a page containing JavaScript, make sure to pass in
`enableJavaScript: true`, or else the launch method will not work properly. On
iOS, the default behavior is to open all web URLs within the app. Everything
else is redirected to the app handler.
else is redirected to the app handler.
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/plugins/tree/master/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.0.6
version: 6.0.7

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down