New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WebView] Does not follow redirects. #25351
Comments
Experiencing the same issue! Any update on this issue? |
@knitero upvoting the initial comment would rise the priority. |
This is just a little hack for android for this issue. not to use in production because i don't know the side effect of this code. taken from this answer https://stackoverflow.com/a/33264935/7416454 steps:- step1:- Clone webview_flutter repo. package io.flutter.plugins.webviewflutter;
import android.content.Context;
import android.view.View;
import android.webkit.WebView;
+ import android.webkit.WebViewClient;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.platform.PlatformView;
import java.util.List;
import java.util.Map;
public class FlutterWebView implements PlatformView, MethodCallHandler {
private static final String JS_CHANNEL_NAMES_FIELD = "javascriptChannelNames";
private final WebView webView;
private final MethodChannel methodChannel;
@SuppressWarnings("unchecked")
FlutterWebView(Context context, BinaryMessenger messenger, int id, Map<String, Object> params) {
webView = new WebView(context);
+ webView.setWebViewClient(new WebViewClient() {
+ @Override
+ public boolean shouldOverrideUrlLoading(WebView view, String url) {
+ return false;
+ }
+ });
methodChannel = new MethodChannel(messenger, "plugins.flutter.io/webview_" + id);
methodChannel.setMethodCallHandler(this);
applySettings((Map<String, Object>) params.get("settings"));
if (params.containsKey(JS_CHANNEL_NAMES_FIELD)) {
registerJavaScriptChannelNames((List<String>) params.get(JS_CHANNEL_NAMES_FIELD));
}
if (params.containsKey("initialUrl")) {
String url = (String) params.get("initialUrl");
webView.loadUrl(url);
}
}
@Override
public View getView() {
return webView;
}
@Override
public void onMethodCall(MethodCall methodCall, Result result) {
switch (methodCall.method) {
case "loadUrl":
loadUrl(methodCall, result);
break;
case "updateSettings":
updateSettings(methodCall, result);
break;
case "canGoBack":
canGoBack(methodCall, result);
break;
case "canGoForward":
canGoForward(methodCall, result);
break;
case "goBack":
goBack(methodCall, result);
break;
case "goForward":
goForward(methodCall, result);
break;
case "reload":
reload(methodCall, result);
break;
case "currentUrl":
currentUrl(methodCall, result);
break;
case "evaluateJavascript":
evaluateJavaScript(methodCall, result);
break;
case "addJavascriptChannels":
addJavaScriptChannels(methodCall, result);
break;
case "removeJavascriptChannels":
removeJavaScriptChannels(methodCall, result);
break;
default:
result.notImplemented();
}
}
private void loadUrl(MethodCall methodCall, Result result) {
String url = (String) methodCall.arguments;
webView.loadUrl(url);
result.success(null);
}
private void doJavascriptRedirect(MethodCall methodCall, Result result) {
String url = (String) methodCall.arguments;
webView.loadUrl(url);
result.success(null);
}
private void canGoBack(MethodCall methodCall, Result result) {
result.success(webView.canGoBack());
}
private void canGoForward(MethodCall methodCall, Result result) {
result.success(webView.canGoForward());
}
private void goBack(MethodCall methodCall, Result result) {
if (webView.canGoBack()) {
webView.goBack();
}
result.success(null);
}
private void goForward(MethodCall methodCall, Result result) {
if (webView.canGoForward()) {
webView.goForward();
}
result.success(null);
}
private void reload(MethodCall methodCall, Result result) {
webView.reload();
result.success(null);
}
private void currentUrl(MethodCall methodCall, Result result) {
result.success(webView.getUrl());
}
@SuppressWarnings("unchecked")
private void updateSettings(MethodCall methodCall, Result result) {
applySettings((Map<String, Object>) methodCall.arguments);
result.success(null);
}
private void evaluateJavaScript(MethodCall methodCall, final Result result) {
String jsString = (String) methodCall.arguments;
if (jsString == null) {
throw new UnsupportedOperationException("JavaScript string cannot be null");
}
webView.evaluateJavascript(
jsString,
new android.webkit.ValueCallback<String>() {
@Override
public void onReceiveValue(String value) {
result.success(value);
}
});
}
@SuppressWarnings("unchecked")
private void addJavaScriptChannels(MethodCall methodCall, Result result) {
List<String> channelNames = (List<String>) methodCall.arguments;
registerJavaScriptChannelNames(channelNames);
result.success(null);
}
@SuppressWarnings("unchecked")
private void removeJavaScriptChannels(MethodCall methodCall, Result result) {
List<String> channelNames = (List<String>) methodCall.arguments;
for (String channelName : channelNames) {
webView.removeJavascriptInterface(channelName);
}
result.success(null);
}
private void applySettings(Map<String, Object> settings) {
for (String key : settings.keySet()) {
switch (key) {
case "jsMode":
updateJsMode((Integer) settings.get(key));
break;
default:
throw new IllegalArgumentException("Unknown WebView setting: " + key);
}
}
}
private void updateJsMode(int mode) {
switch (mode) {
case 0: // disabled
webView.getSettings().setJavaScriptEnabled(false);
break;
case 1: // unrestricted
webView.getSettings().setJavaScriptEnabled(true);
break;
default:
throw new IllegalArgumentException("Trying to set unknown JavaScript mode: " + mode);
}
}
private void registerJavaScriptChannelNames(List<String> channelNames) {
for (String channelName : channelNames) {
webView.addJavascriptInterface(
new JavaScriptChannel(methodChannel, channelName), channelName);
}
}
@Override
public void dispose() {}
} step3:- copy updated plugin in your project and update path in pubspec.yaml like this webview_flutter: Note:- webview_flutter is the plugin folder inside the apps root directory /myapp/webview_flutter |
PS. server side redirects are working |
@zoechi can i make a pull request regarding this ? |
@rakeshlanjewarwork sure, go ahead 👍 |
thanks , pushing... |
@rakeshlanjewarwork We are waiting for your pull request, thank you. |
i have made a pull request on same day. flutter/plugins#1197 |
Could you try VS CODE - Command - Format Document ? |
@rakeshlanjewarwork I also need this fix , could you try again ? Thank you very much. |
@realcarlos I have tried to pull request but due to some problems about formatting of code the CI build is failing. If you can make a pull please see here flutter/plugins#1243 |
Need this fix badly! |
@zoechi could you Kindly help to merge it? thank you |
Hi all, I just ran into this same issue, and it looks like the underlying issue is that these are redirects from HTTP to HTTPS. On iOS, for example, we have to add these domains to our App Transport Security exception list (e.g.: https://stackoverflow.com/questions/30731785/how-do-i-load-an-http-url-with-app-transport-security-enabled-in-ios-9) |
@skylineproject same issue here (just tested on Android) can anyone tell me what they did to fix? |
I just tested redirects with webview_flutter 0.3.10+3 and it's working for me. I tested on Android O and Android M devices (to cover both the Loading https://flutter.io which redirects to https://flutter.dev works for me on both Android and iOS. As @skylineproject mentioned in #25351 (comment) iOS is not allowing http connections by default, but you can add an Info.plist key to enable it. With the Info.plist key loading http://flutter.io which redirects to https://flutter.dev worked on all tested devices. @pedrosalesmiranda which webview_flutter version are you using? which device and OS version? can you share a sample url that's not loading for you? |
My bad here. I tought ^0.1.1 was getting the last version of minor but nop. So i just changed .yaml to webview_flutter: 0.3.10+3 |
Great, I'm closing this issue then. |
Hi all, I do not know if my issue related to redirections, problem is that I can not open simple html page on my server which is not HTTPS (for example http://<MY_IP_ADDRESS>/stats.html) but it can be opened in browser. Also I've tested example on Android https://github.com/flutter/plugins/tree/master/packages/webview_flutter/example it's also not work with initial URL 'http://google.com' |
@mavr301 to load http sites on iOS you need to add new key to ios/Runner/Info.plist |
https://www.pleatsbouttique.com |
Worked for me, thx! |
I'm using webview_flutter 0.3.18+1 if url has 302 redirect android will be shown Webpage not available. net::ERR_UNKNOWN_URL_SCHEME https://forms.gle/UYTCutLKbSGdDUw99 Not everything doesn't work. here is CURL exceute result
any idea? thank you :) |
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of |
The webview_flutter plugin does not appear to follow redirects. It simply displays a blank white page. This is an example of a URL that has a redirect for the flutter.io website. (this is a Google Ad link)
https://www.googleadservices.com/pagead/aclk?sa=L&ai=DChcSEwis9JrFup7fAhVbuMAKHTNtA5gYABAAGgJpbQ&ohost=www.google.com&cid=CAESQeD2UsU5H7fGhUZOPtY-kPyuOuLkiFu02AYGI7ZI1pHvfjhZaQhzWkZiIC9SuCSp_y7E9WnDpUNypnNzx0oBS6Nm&sig=AOD64_2AaG88ZD2HKakgg_Zdck7rnkTkZA&q=&ved=2ahUKEwjrpJHFup7fAhXMi60KHSYZD4QQ0Qx6BAgPEAE&adurl=
The text was updated successfully, but these errors were encountered: