You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Copy file name to clipboardExpand all lines: packages/url_launcher/url_launcher/README.md
+57-49Lines changed: 57 additions & 49 deletions
Original file line number
Diff line number
Diff line change
@@ -18,23 +18,23 @@ To use this plugin, add `url_launcher` as a [dependency in your pubspec.yaml fil
18
18
import 'package:flutter/material.dart';
19
19
import 'package:url_launcher/url_launcher.dart';
20
20
21
-
const String _url = 'https://flutter.dev';
21
+
final Uri _url = Uri.parse('https://flutter.dev');
22
22
23
23
void main() => runApp(
24
24
const MaterialApp(
25
25
home: Material(
26
26
child: Center(
27
27
child: RaisedButton(
28
-
onPressed: _launchURL,
28
+
onPressed: _launchUrl,
29
29
child: Text('Show Flutter homepage'),
30
30
),
31
31
),
32
32
),
33
33
),
34
34
);
35
35
36
-
void _launchURL() async {
37
-
if (!await launch(_url)) throw 'Could not launch $_url';
36
+
void _launchUrl() async {
37
+
if (!await launchUrl(_url)) throw 'Could not launch $_url';
38
38
}
39
39
```
40
40
@@ -43,7 +43,7 @@ See the example app for more complex examples.
43
43
## Configuration
44
44
45
45
### iOS
46
-
Add any URL schemes passed to `canLaunch` as `LSApplicationQueriesSchemes` entries in your Info.plist file.
46
+
Add any URL schemes passed to `canLaunchUrl` as `LSApplicationQueriesSchemes` entries in your Info.plist file.
47
47
48
48
Example:
49
49
```
@@ -59,7 +59,7 @@ See [`-[UIApplication canOpenURL:]`](https://developer.apple.com/documentation/u
59
59
### Android
60
60
61
61
Starting from API 30 Android requires package visibility configuration in your
62
-
`AndroidManifest.xml` otherwise `canLaunch` will return `false`. A `<queries>`
62
+
`AndroidManifest.xml` otherwise `canLaunchUrl` will return `false`. A `<queries>`
63
63
element must be added to your manifest as a child of the root element.
64
64
65
65
The snippet below shows an example for an application that uses `https`, `tel`,
@@ -94,34 +94,53 @@ for examples of other queries.
94
94
95
95
## Supported URL schemes
96
96
97
-
The [`launch`](https://pub.dev/documentation/url_launcher/latest/url_launcher/launch.html) method
98
-
takes a string argument containing a URL. This URL
99
-
can be formatted using a number of different URL schemes. The supported
100
-
URL schemes depend on the underlying platform and installed apps.
97
+
The provided URL is passed directly to the host platform for handling. The
98
+
supported URL schemes therefore depend on the platform and installed apps.
101
99
102
100
Commonly used schemes include:
103
101
104
102
| Scheme | Example | Action |
105
103
|:---|:---|:---|
106
-
|`https:<URL>`|`https://flutter.dev`| Open URL in the default browser |
107
-
|`mailto:<email address>?subject=<subject>&body=<body>`|`mailto:[email protected]?subject=News&body=New%20plugin`| Create email to <emailaddress> in the default email app |
108
-
|`tel:<phone number>`|`tel:+1-555-010-999`| Make a phone call to <phonenumber> using the default phone app |
109
-
|`sms:<phone number>`|`sms:5550101234`| Send an SMS message to <phonenumber> using the default messaging app |
104
+
|`https:<URL>`|`https://flutter.dev`| Open `<URL>` in the default browser |
105
+
|`mailto:<email address>?subject=<subject>&body=<body>`|`mailto:[email protected]?subject=News&body=New%20plugin`| Create email to `<email address>` in the default email app |
106
+
|`tel:<phone number>`|`tel:+1-555-010-999`| Make a phone call to `<phone number>` using the default phone app |
107
+
|`sms:<phone number>`|`sms:5550101234`| Send an SMS message to `<phone number>` using the default messaging app |
110
108
|`file:<path>`|`file:/home`| Open file or folder using default app association, supported on desktop platforms |
111
109
112
110
More details can be found here for [iOS](https://developer.apple.com/library/content/featuredarticles/iPhoneURLScheme_Reference/Introduction/Introduction.html)
113
111
and [Android](https://developer.android.com/guide/components/intents-common.html)
114
112
115
-
**Note**: URL schemes are only supported if there are apps installed on the device that can
113
+
URL schemes are only supported if there are apps installed on the device that can
116
114
support them. For example, iOS simulators don't have a default email or phone
117
115
apps installed, so can't open `tel:` or `mailto:` links.
118
116
117
+
### Checking supported schemes
118
+
119
+
If you need to know at runtime whether a scheme is guaranteed to work before
120
+
using it (for instance, to adjust your UI based on what is available), you can
121
+
check with [`canLaunchUrl`](https://pub.dev/documentation/url_launcher/latest/url_launcher/canLaunchUrl.html).
122
+
123
+
However, `canLaunchUrl` can return false even if `launchUrl` would work in
124
+
some circumstances (in web applications, on mobile without the necessary
125
+
configuration as described above, etc.), so in cases where you can provide
126
+
fallback behavior it is better to use `launchUrl` directly and handle failure.
127
+
For example, a UI button that would have sent feedback email using a `mailto` URL
128
+
might instead open a web-based feedback form using an `https` URL on failure,
129
+
rather than disabling the button if `canLaunchUrl` returns false for `mailto`.
130
+
119
131
### Encoding URLs
120
132
121
133
URLs must be properly encoded, especially when including spaces or other special
122
-
characters. This can be done using the
134
+
characters. In general this is handled automatically by the
0 commit comments