Reputation: 419
I'm trying to sign in to an app via WebView, and at the end of the process it gives me a response with a token. How can I get the response data from the WebView? Any workarounds for this?
Upvotes: 2
Views: 5308
Reputation: 868
Using webview_flutter you can get your response by injecting javascript like that:
late WebViewController _controller;
your webview widget
WebView(
initialUrl: YOUR_URL,
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (controller) {
_controller = controller;
},
onPageFinished: (finish) {
//reading response on finish
final response = await _controller.runJavascriptReturningResult("document.documentElement.innerText");
print(jsonDecode(response)); //don't forget to decode into json
},
),
Upvotes: 2
Reputation: 1298
You can launch request url in a webview and inject javascript to get response. Example code using flutter webview plugin:
final webView = FlutterWebviewPlugin();
webView.launch(requestUrl); //hidden: true if necessary
webView.onStateChanged.listen((event) async {
if (event.type == WebViewState.finishLoad) {
final response = await webView
.evalJavascript("document.documentElement.innerText");
print(response);
}
);
Upvotes: 6