Skip to content
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

Ability to intercept URL #293

Closed
SimplyJaymin opened this issue Jan 9, 2019 · 7 comments
Closed

Ability to intercept URL #293

SimplyJaymin opened this issue Jan 9, 2019 · 7 comments

Comments

@SimplyJaymin
Copy link

Hello,
How can I intercept a URL before it is loaded into the webview & stop loading?
I tried the following -
_onStateChanged = flutterWebviewPlugin.onStateChanged.listen((WebViewStateChanged state) { if (state.url.startsWith("someScheme")) { flutterWebviewPlugin.stopLoading(); } });

But the onStateChanged is called after the url has loaded into the webview, so I cannot stop the loading.

@mohammedmunaf
Copy link

I have the same issue. Pease help!

@JannieT
Copy link

JannieT commented Apr 25, 2019

You can intercept on the onUrlChanged event which fires before the WebViewState.startLoad state change. I am using it like this:

webview.onUrlChanged.listen( (String url) async {
    if (!mounted) return;
    
    if (!_isSpecialUrl(url)) return;

    if (await canLaunch(url)) {
      print('diverting: $url');
      webview.stopLoading();
      await launch(url);
    }
    
  });

@mohammedmunaf
Copy link

@JannieT No use. Webview doesn't stop loading. callback come to onUrlChanged once the loading already starts, not before loading.

@JannieT
Copy link

JannieT commented Apr 26, 2019

@mohammed90 works great for me on Flutter v1.2.1, Plugin v0.3.3

This is my event trace:

flutter: onStateChanged: WebViewState.shouldStart https://redacted.com/notes
flutter: onUrlChanged: https://redacted.com/notes
flutter: onStateChanged: WebViewState.startLoad https://redacted.com/notes
flutter: onHttpError: 200 https://redacted.com/notes
flutter: onStateChanged: WebViewState.finishLoad https://redacted.com/notes

@mohammedmunaf
Copy link

mohammedmunaf commented Apr 26, 2019

@JannieT I am also using Plugin v0.3.3 but flutter 1.0.0+1
Below is my console log onUrlChanged is called after WebViewState.startLoad

I/flutter (21612): WebViewState.shouldStart
I/flutter (21612): WebViewState.startLoad
I/flutter (21612): onUrlChanged
I/flutter (21612): WebViewState.finishLoad

@JannieT
Copy link

JannieT commented Apr 26, 2019

@mohammedmunaf have you tried intercepting at WebViewState.shouldStart?

@mohammedmunaf
Copy link

mohammedmunaf commented Apr 26, 2019

Finally this worked for me! Happy coding 👍 Thanks @JannieT

flutterWebviewPlugin.onStateChanged.listen((WebViewStateChanged state) async {
      if (state.type==WebViewState.shouldStart){
         if(state.url.startsWith('https://www.google.com')){
            flutterWebviewPlugin.stopLoading();
          } 
      }
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants