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

Commit 940c10c

Browse files
authored
[url_launcher_web] Adds "tel" and "sms" URL support (#2847)
1 parent 388c66e commit 940c10c

File tree

4 files changed

+82
-8
lines changed

4 files changed

+82
-8
lines changed

packages/url_launcher/url_launcher_web/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.1.2
2+
3+
- Adds "tel" and "sms" support
4+
15
# 0.1.1+6
26

37
- Open "mailto" urls with target set as "\_top" on Safari browsers.

packages/url_launcher/url_launcher_web/lib/url_launcher_web.dart

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import 'package:url_launcher_platform_interface/url_launcher_platform_interface.
77

88
import 'package:platform_detect/platform_detect.dart' show browser;
99

10-
const _mailtoScheme = 'mailto';
10+
const _safariTargetTopSchemes = {
11+
'mailto',
12+
'tel',
13+
'sms',
14+
};
1115

1216
/// The web implementation of [UrlLauncherPlatform].
1317
///
@@ -16,7 +20,10 @@ class UrlLauncherPlugin extends UrlLauncherPlatform {
1620
html.Window _window;
1721

1822
// The set of schemes that can be handled by the plugin
19-
static final _supportedSchemes = {'http', 'https', _mailtoScheme};
23+
static final _supportedSchemes = {
24+
'http',
25+
'https',
26+
}.union(_safariTargetTopSchemes);
2027

2128
/// A constructor that allows tests to override the window object used by the plugin.
2229
UrlLauncherPlugin({@visibleForTesting html.Window window})
@@ -29,16 +36,18 @@ class UrlLauncherPlugin extends UrlLauncherPlatform {
2936

3037
String _getUrlScheme(String url) => Uri.tryParse(url)?.scheme;
3138

32-
bool _isMailtoScheme(String url) => _getUrlScheme(url) == _mailtoScheme;
39+
bool _isSafariTargetTopScheme(String url) =>
40+
_safariTargetTopSchemes.contains(_getUrlScheme(url));
3341

3442
/// Opens the given [url] in a new window.
3543
///
3644
/// Returns the newly created window.
3745
@visibleForTesting
3846
html.WindowBase openNewWindow(String url) {
39-
// We need to open mailto urls on the _top window context on safari browsers.
47+
// We need to open mailto, tel and sms urls on the _top window context on safari browsers.
4048
// See https://github.com/flutter/flutter/issues/51461 for reference.
41-
final target = browser.isSafari && _isMailtoScheme(url) ? '_top' : '';
49+
final target =
50+
browser.isSafari && _isSafariTargetTopScheme(url) ? '_top' : '';
4251
return _window.open(url, target);
4352
}
4453

packages/url_launcher/url_launcher_web/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher/u
44
# 0.1.y+z is compatible with 1.0.0, if you land a breaking change bump
55
# the version to 2.0.0.
66
# See more details: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0
7-
version: 0.1.1+6
7+
version: 0.1.2
88

99
flutter:
1010
plugin:

packages/url_launcher/url_launcher_web/test/url_launcher_web_test.dart

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,13 @@ void main() {
3636
plugin.canLaunch('mailto:name@mydomain.com'), completion(isTrue));
3737
});
3838

39-
test('"tel" URLs -> false', () {
40-
expect(plugin.canLaunch('tel:5551234567'), completion(isFalse));
39+
test('"tel" URLs -> true', () {
40+
expect(plugin.canLaunch('tel:5551234567'), completion(isTrue));
41+
});
42+
43+
test('"sms" URLs -> true', () {
44+
expect(plugin.canLaunch('sms:+19725551212?body=hello%20there'),
45+
completion(isTrue));
4146
});
4247
});
4348

@@ -48,6 +53,9 @@ void main() {
4853
.thenReturn(MockWindow());
4954
when(mockWindow.open('mailto:name@mydomain.com', ''))
5055
.thenReturn(MockWindow());
56+
when(mockWindow.open('tel:5551234567', '')).thenReturn(MockWindow());
57+
when(mockWindow.open('sms:+19725551212?body=hello%20there', ''))
58+
.thenReturn(MockWindow());
5159
});
5260

5361
test('launching a URL returns true', () {
@@ -77,6 +85,34 @@ void main() {
7785
),
7886
completion(isTrue));
7987
});
88+
89+
test('launching a "tel" returns true', () {
90+
expect(
91+
plugin.launch(
92+
'tel:5551234567',
93+
useSafariVC: null,
94+
useWebView: null,
95+
universalLinksOnly: null,
96+
enableDomStorage: null,
97+
enableJavaScript: null,
98+
headers: null,
99+
),
100+
completion(isTrue));
101+
});
102+
103+
test('launching a "sms" returns true', () {
104+
expect(
105+
plugin.launch(
106+
'sms:+19725551212?body=hello%20there',
107+
useSafariVC: null,
108+
useWebView: null,
109+
universalLinksOnly: null,
110+
enableDomStorage: null,
111+
enableJavaScript: null,
112+
headers: null,
113+
),
114+
completion(isTrue));
115+
});
80116
});
81117

82118
group('openNewWindow', () {
@@ -98,6 +134,18 @@ void main() {
98134
verify(mockWindow.open('mailto:name@mydomain.com', ''));
99135
});
100136

137+
test('tel urls should be launched on a new window', () {
138+
plugin.openNewWindow('tel:5551234567');
139+
140+
verify(mockWindow.open('tel:5551234567', ''));
141+
});
142+
143+
test('sms urls should be launched on a new window', () {
144+
plugin.openNewWindow('sms:+19725551212?body=hello%20there');
145+
146+
verify(mockWindow.open('sms:+19725551212?body=hello%20there', ''));
147+
});
148+
101149
group('Safari', () {
102150
setUp(() {
103151
platform.configurePlatformForTesting(browser: platform.safari);
@@ -120,6 +168,19 @@ void main() {
120168

121169
verify(mockWindow.open('mailto:name@mydomain.com', '_top'));
122170
});
171+
172+
test('tel urls should be launched on the same window', () {
173+
plugin.openNewWindow('tel:5551234567');
174+
175+
verify(mockWindow.open('tel:5551234567', '_top'));
176+
});
177+
178+
test('sms urls should be launched on the same window', () {
179+
plugin.openNewWindow('sms:+19725551212?body=hello%20there');
180+
181+
verify(
182+
mockWindow.open('sms:+19725551212?body=hello%20there', '_top'));
183+
});
123184
});
124185
});
125186
});

0 commit comments

Comments
 (0)