7
7
import 'dart:async' ;
8
8
9
9
import 'package:flutter/material.dart' ;
10
- import 'package:url_launcher/link.dart' ;
11
- import 'package:url_launcher/url_launcher.dart' ;
10
+ import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart' ;
12
11
13
12
void main () {
14
13
runApp (MyApp ());
@@ -40,11 +39,15 @@ class _MyHomePageState extends State<MyHomePage> {
40
39
String _phone = '' ;
41
40
42
41
Future <void > _launchInBrowser (String url) async {
43
- if (await canLaunch (url)) {
44
- await launch (
42
+ UrlLauncherPlatform launcher = UrlLauncherPlatform .instance;
43
+ if (await launcher.canLaunch (url)) {
44
+ await launcher.launch (
45
45
url,
46
- forceSafariVC: false ,
47
- forceWebView: false ,
46
+ useSafariVC: false ,
47
+ useWebView: false ,
48
+ enableJavaScript: false ,
49
+ enableDomStorage: false ,
50
+ universalLinksOnly: false ,
48
51
headers: < String , String > {'my_header_key' : 'my_header_value' },
49
52
);
50
53
} else {
@@ -53,11 +56,15 @@ class _MyHomePageState extends State<MyHomePage> {
53
56
}
54
57
55
58
Future <void > _launchInWebViewOrVC (String url) async {
56
- if (await canLaunch (url)) {
57
- await launch (
59
+ UrlLauncherPlatform launcher = UrlLauncherPlatform .instance;
60
+ if (await launcher.canLaunch (url)) {
61
+ await launcher.launch (
58
62
url,
59
- forceSafariVC: true ,
60
- forceWebView: true ,
63
+ useSafariVC: true ,
64
+ useWebView: true ,
65
+ enableJavaScript: false ,
66
+ enableDomStorage: false ,
67
+ universalLinksOnly: false ,
61
68
headers: < String , String > {'my_header_key' : 'my_header_value' },
62
69
);
63
70
} else {
@@ -66,47 +73,39 @@ class _MyHomePageState extends State<MyHomePage> {
66
73
}
67
74
68
75
Future <void > _launchInWebViewWithJavaScript (String url) async {
69
- if (await canLaunch (url)) {
70
- await launch (
76
+ UrlLauncherPlatform launcher = UrlLauncherPlatform .instance;
77
+ if (await launcher.canLaunch (url)) {
78
+ await launcher.launch (
71
79
url,
72
- forceSafariVC : true ,
73
- forceWebView : true ,
80
+ useSafariVC : true ,
81
+ useWebView : true ,
74
82
enableJavaScript: true ,
83
+ enableDomStorage: false ,
84
+ universalLinksOnly: false ,
85
+ headers: < String , String > {},
75
86
);
76
87
} else {
77
88
throw 'Could not launch $url ' ;
78
89
}
79
90
}
80
91
81
92
Future <void > _launchInWebViewWithDomStorage (String url) async {
82
- if (await canLaunch (url)) {
83
- await launch (
93
+ UrlLauncherPlatform launcher = UrlLauncherPlatform .instance;
94
+ if (await launcher.canLaunch (url)) {
95
+ await launcher.launch (
84
96
url,
85
- forceSafariVC: true ,
86
- forceWebView: true ,
97
+ useSafariVC: true ,
98
+ useWebView: true ,
99
+ enableJavaScript: false ,
87
100
enableDomStorage: true ,
101
+ universalLinksOnly: false ,
102
+ headers: < String , String > {},
88
103
);
89
104
} else {
90
105
throw 'Could not launch $url ' ;
91
106
}
92
107
}
93
108
94
- Future <void > _launchUniversalLinkIos (String url) async {
95
- if (await canLaunch (url)) {
96
- final bool nativeAppLaunchSucceeded = await launch (
97
- url,
98
- forceSafariVC: false ,
99
- universalLinksOnly: true ,
100
- );
101
- if (! nativeAppLaunchSucceeded) {
102
- await launch (
103
- url,
104
- forceSafariVC: true ,
105
- );
106
- }
107
- }
108
- }
109
-
110
109
Widget _launchStatus (BuildContext context, AsyncSnapshot <void > snapshot) {
111
110
if (snapshot.hasError) {
112
111
return Text ('Error: ${snapshot .error }' );
@@ -116,8 +115,17 @@ class _MyHomePageState extends State<MyHomePage> {
116
115
}
117
116
118
117
Future <void > _makePhoneCall (String url) async {
119
- if (await canLaunch (url)) {
120
- await launch (url);
118
+ UrlLauncherPlatform launcher = UrlLauncherPlatform .instance;
119
+ if (await launcher.canLaunch (url)) {
120
+ await launcher.launch (
121
+ url,
122
+ useSafariVC: false ,
123
+ useWebView: false ,
124
+ enableJavaScript: false ,
125
+ enableDomStorage: false ,
126
+ universalLinksOnly: true ,
127
+ headers: < String , String > {},
128
+ );
121
129
} else {
122
130
throw 'Could not launch $url ' ;
123
131
}
@@ -178,38 +186,17 @@ class _MyHomePageState extends State<MyHomePage> {
178
186
child: const Text ('Launch in app(DOM storage ON)' ),
179
187
),
180
188
const Padding (padding: EdgeInsets .all (16.0 )),
181
- ElevatedButton (
182
- onPressed: () => setState (() {
183
- _launched = _launchUniversalLinkIos (toLaunch);
184
- }),
185
- child: const Text (
186
- 'Launch a universal link in a native app, fallback to Safari.(Youtube)' ),
187
- ),
188
- const Padding (padding: EdgeInsets .all (16.0 )),
189
189
ElevatedButton (
190
190
onPressed: () => setState (() {
191
191
_launched = _launchInWebViewOrVC (toLaunch);
192
192
Timer (const Duration (seconds: 5 ), () {
193
193
print ('Closing WebView after 5 seconds...' );
194
- closeWebView ();
194
+ UrlLauncherPlatform .instance. closeWebView ();
195
195
});
196
196
}),
197
197
child: const Text ('Launch in app + close after 5 seconds' ),
198
198
),
199
199
const Padding (padding: EdgeInsets .all (16.0 )),
200
- Link (
201
- uri: Uri .parse (
202
- 'https://pub.dev/documentation/url_launcher/latest/link/link-library.html' ),
203
- target: LinkTarget .blank,
204
- builder: (ctx, openLink) {
205
- return TextButton .icon (
206
- onPressed: openLink,
207
- label: Text ('Link Widget documentation' ),
208
- icon: Icon (Icons .read_more),
209
- );
210
- },
211
- ),
212
- const Padding (padding: EdgeInsets .all (16.0 )),
213
200
FutureBuilder <void >(future: _launched, builder: _launchStatus),
214
201
],
215
202
),
0 commit comments