Simon
Simon

Reputation: 514

Flutter iOS Webview opens every link on page

I'm working on a WebView application for a website with posts. When I click on a posted item, I want my application to open a new screen. On Android it works like a charm. But on iOS there is something weird going on.

For some reason the app opens a new screen every time. See the video with this link.

Somehow the navigationDelegate is called on every link it encounters. When I look in the logs, I mainly see that they are links to advertisements or something like that.

Is there a way around or something similar to solve this problem?

The WebView widget code:

WebView(
  key: _key,
  initialUrl: startUrl,
  navigationDelegate: (NavigationRequest request) {
    debugPrint("navigationDelegate: " + request.toString());
    if (request.url.startsWith(Constants.domainName)) {
      Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => PostView(url: request.url)));
      return NavigationDecision.navigate;
    } else {
      _launchURL(Uri.parse(request.url));
      return NavigationDecision.prevent;
    }
  },
);

Upvotes: 2

Views: 1654

Answers (1)

Sokleng Bun
Sokleng Bun

Reputation: 11

i have the same issue too, but i managed to solve it. this is how i solve it.

    WebView(
      key: _key,
      initialUrl: startUrl,
      navigationDelegate: (NavigationRequest request) {
        debugPrint("navigationDelegate: " + request.toString());
        if (request.url.startsWith(Constants.domainName)) {
          // what you need to add
          if(Platform.isIOS){
              if (request.url != startUrl && request.isForMainFrame) {
                 Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => PostView(url: request.url)));
                    return NavigationDecision.prevent;
              }
              return NavigationDecision.navigate;
          }
          // -----------------
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => PostView(url: request.url)));
          return NavigationDecision.navigate;
        } else {
          _launchURL(Uri.parse(request.url));
          return NavigationDecision.prevent;
        }
      },
    );

if check only isForMainFrame, it will push startUrl to PostView(). also check the same thing in PostView() as well if it 's also a webview.

Upvotes: 1

Related Questions