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

Commit e224bb9

Browse files
committed
[url_launcher]: add option to enable DOM storage in android webview
1 parent fc10d44 commit e224bb9

6 files changed

Lines changed: 59 additions & 2 deletions

File tree

packages/url_launcher/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 5.0.5
2+
3+
* Add `enableDomStorage` field to `launch` to enable DOM storage in Android WebView.
4+
15
## 5.0.4
26

37
* Update Dart code to conform to current Dart formatter.

packages/url_launcher/android/src/main/java/io/flutter/plugins/urllauncher/UrlLauncherPlugin.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ private void launch(MethodCall call, Result result, String url) {
6868
Intent launchIntent;
6969
boolean useWebView = call.argument("useWebView");
7070
boolean enableJavaScript = call.argument("enableJavaScript");
71+
boolean enableDomStorage = call.argument("enableDomStorage");
7172
Activity activity = mRegistrar.activity();
7273
if (activity == null) {
7374
result.error("NO_ACTIVITY", "Launching a URL requires a foreground activity.", null);
@@ -77,6 +78,7 @@ private void launch(MethodCall call, Result result, String url) {
7778
launchIntent = new Intent(activity, WebViewActivity.class);
7879
launchIntent.putExtra("url", url);
7980
launchIntent.putExtra("enableJavaScript", enableJavaScript);
81+
launchIntent.putExtra("enableDomStorage", enableDomStorage);
8082
} else {
8183
launchIntent = new Intent(Intent.ACTION_VIEW);
8284
launchIntent.setData(Uri.parse(url));
@@ -105,10 +107,14 @@ public void onCreate(Bundle savedInstanceState) {
105107
Intent intent = getIntent();
106108
String url = intent.getStringExtra("url");
107109
Boolean enableJavaScript = intent.getBooleanExtra("enableJavaScript", false);
110+
Boolean enableDomStorage = intent.getBooleanExtra("enableDomStorage", false);
108111
webview.loadUrl(url);
109112
if (enableJavaScript) {
110113
webview.getSettings().setJavaScriptEnabled(enableJavaScript);
111114
}
115+
if (enableDomStorage) {
116+
webview.getSettings().setDomStorageEnabled(enableDomStorage);
117+
}
112118
// Open new urls inside the webview itself.
113119
webview.setWebViewClient(
114120
new WebViewClient() {

packages/url_launcher/example/lib/main.dart

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ class _MyHomePageState extends State<MyHomePage> {
6565
}
6666
}
6767

68+
Future<void> _launchInWebViewWithDomStorage(String url) async {
69+
if (await canLaunch(url)) {
70+
await launch(
71+
url,
72+
forceSafariVC: true,
73+
forceWebView: true,
74+
enableDomStorage: true,
75+
);
76+
} else {
77+
throw 'Could not launch $url';
78+
}
79+
}
80+
6881
Future<void> _launchUniversalLinkIos(String url) async {
6982
if (await canLaunch('https://youtube.com')) {
7083
final bool nativeAppLaunchSucceeded = await launch(
@@ -138,13 +151,19 @@ class _MyHomePageState extends State<MyHomePage> {
138151
}),
139152
child: const Text('Launch in app'),
140153
),
141-
const Padding(padding: EdgeInsets.all(16.0)),
142154
RaisedButton(
143155
onPressed: () => setState(() {
144156
_launched = _launchInWebViewWithJavaScript(toLaunch);
145157
}),
146158
child: const Text('Launch in app(JavaScript ON)'),
147159
),
160+
RaisedButton(
161+
onPressed: () => setState(() {
162+
_launched = _launchInWebViewWithDomStorage(toLaunch);
163+
}),
164+
child: const Text('Launch in app(DOM storage ON)'),
165+
),
166+
const Padding(padding: EdgeInsets.all(16.0)),
148167
RaisedButton(
149168
onPressed: () => setState(() {
150169
_launched = _launchUniversalLinkIos(toLaunch);

packages/url_launcher/lib/url_launcher.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const MethodChannel _channel = MethodChannel('plugins.flutter.io/url_launcher');
4242
/// WebViews.
4343
/// [enableJavaScript] is an Android only setting. If true, WebView enable
4444
/// javascript.
45+
/// [enableDomStorage] is an Android only setting. If true, WebView enable
46+
/// DOM storage.
4547
///
4648
/// Note that if any of the above are set to true but the URL is not a web URL,
4749
/// this will throw a [PlatformException].
@@ -57,6 +59,7 @@ Future<bool> launch(
5759
bool forceSafariVC,
5860
bool forceWebView,
5961
bool enableJavaScript,
62+
bool enableDomStorage,
6063
bool universalLinksOnly,
6164
Brightness statusBarBrightness,
6265
}) async {
@@ -86,6 +89,7 @@ Future<bool> launch(
8689
'useSafariVC': forceSafariVC ?? isWebURL,
8790
'useWebView': forceWebView ?? false,
8891
'enableJavaScript': enableJavaScript ?? false,
92+
'enableDomStorage': enableDomStorage ?? false,
8993
'universalLinksOnly': universalLinksOnly ?? false,
9094
},
9195
);

packages/url_launcher/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for launching a URL on Android and iOS. Supports
33
web, phone, SMS, and email schemes.
44
author: Flutter Team <flutter-dev@googlegroups.com>
55
homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher
6-
version: 5.0.4
6+
version: 5.0.5
77

88
flutter:
99
plugin:

packages/url_launcher/test/url_launcher_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ void main() {
4040
'useSafariVC': true,
4141
'useWebView': false,
4242
'enableJavaScript': false,
43+
'enableDomStorage': false,
4344
'universalLinksOnly': false,
4445
})
4546
],
@@ -56,6 +57,7 @@ void main() {
5657
'useSafariVC': true,
5758
'useWebView': false,
5859
'enableJavaScript': false,
60+
'enableDomStorage': false,
5961
'universalLinksOnly': false,
6062
})
6163
],
@@ -73,6 +75,7 @@ void main() {
7375
'useSafariVC': false,
7476
'useWebView': false,
7577
'enableJavaScript': false,
78+
'enableDomStorage': false,
7679
'universalLinksOnly': true,
7780
})
7881
],
@@ -89,6 +92,7 @@ void main() {
8992
'useSafariVC': true,
9093
'useWebView': true,
9194
'enableJavaScript': false,
95+
'enableDomStorage': false,
9296
'universalLinksOnly': false,
9397
})
9498
],
@@ -106,6 +110,25 @@ void main() {
106110
'useSafariVC': true,
107111
'useWebView': true,
108112
'enableJavaScript': true,
113+
'enableDomStorage': false,
114+
'universalLinksOnly': false,
115+
})
116+
],
117+
);
118+
});
119+
120+
test('launch force WebView enable DOM storage', () async {
121+
await launch('http://example.com/',
122+
forceWebView: true, enableDomStorage: true);
123+
expect(
124+
log,
125+
<Matcher>[
126+
isMethodCall('launch', arguments: <String, Object>{
127+
'url': 'http://example.com/',
128+
'useSafariVC': true,
129+
'useWebView': true,
130+
'enableJavaScript': false,
131+
'enableDomStorage': true,
109132
'universalLinksOnly': false,
110133
})
111134
],
@@ -122,6 +145,7 @@ void main() {
122145
'useSafariVC': false,
123146
'useWebView': false,
124147
'enableJavaScript': false,
148+
'enableDomStorage': false,
125149
'universalLinksOnly': false,
126150
})
127151
],

0 commit comments

Comments
 (0)