question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Accessing public methods of Android WebView

See original GitHub issue

Tell us about the problem

I am trying to use the public method setWebContentsDebuggingEnabled() of the Android WebView class, but I’m not sure how to access it. I tried

webView.android.setWebContentsDebuggingEnabled(true);

but it doesn’t work as I get the following error:

webView.android.setWebContentsDebuggingEnabled is not a function

See below for details.

Which platform(s) does your issue occur on?

Android

Please provide the following version numbers that your issue occurs with:

  • CLI: 3.1.3
  • Cross-platform modules: 3.1.1
  • Runtime(s): tns-android 3.1.1
  • Plugin(s): “nativescript-angular”: “4.2.0”, “nativescript-localstorage”: “1.1.2” “nativescript-theme-core”: “1.0.4” “nativescript-css-loader”: “0.26.1” “nativescript-dev-android-snapshot”: “0.0.9” “nativescript-dev-sass”: “1.3.0” “nativescript-dev-webpack”: “0.6.0”

Note: Project based on the seed angular-seed-advanced

Please tell us how to recreate the issue in as much detail as possible.

Here’s the ngAfterViewInit method where I setup the WebView:

    ngAfterViewInit() {
        let webView: WebView = this.webViewRef.nativeElement;

        webView.on(WebView.loadStartedEvent, function() {
            webView.android.getSettings().setBuiltInZoomControls(true);
            webView.android.getSettings().setDisplayZoomControls(false);
            webView.android.setWebContentsDebuggingEnabled(true);
        });

        webView.on(WebView.loadFinishedEvent, function (args: LoadEventData) {
            let message;
            if (!args.error) {
                message = 'WebView finished loading of ' + args.url;
            } else {
                message = 'Error loading ' + args.url + ': ' + args.error;
            }

            console.log('WebView message - ' + message);
        });
    }

If I remove the line with setWebContentsDebuggingEnabled, the webview works. The two methods calls in getSettings() are working well because they are public methods of the Android WebSettings class, which is not the case of setWebContentsDebuggingEnabled().

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10

github_iconTop GitHub Comments

3reactions
Guatomcommented, Apr 11, 2018

Thanks @nmongiya for your help. Your code didn’t exactly work for me, but it was almost there. I did the following:

android.webkit.WebView['setWebContentsDebuggingEnabled'](true);

The initial code @davidsandoz posted didn’t work because the interface doesn’t have that property so TS won’t compile. Guess I’ll be sending a PR to update the interface.

A million thanks!

3reactions
davidsandozcommented, Sep 12, 2017

I still cannot tell why webView.android.setWebContentsDebuggingEnabled(true); is not working, but I eventually figured out that android.webkit.WebView.setWebContentsDebuggingEnabled(true); does the job.

Read more comments on GitHub >

github_iconTop Results From Across the Web

WebView - Android Developers
Public constructors. WebView; WebView; WebView; WebView; WebView. Public methods. addJavascriptInterface; autofill; canGoBack; canGoBackOrForward ...
Read more >
Access to WebView from another function in MainActivity class
You're trying to use view before it has been initialized. You need to wait until after view = (WebView) findViewById(R.id.
Read more >
Android WebView Example Tutorial - DigitalOcean
For HTML code that is limited in terms of scope, we can implement the static method fromHtml() that belongs to the HTML Utility...
Read more >
How to Use WebView in Android? - GeeksforGeeks
WebView is a view that displays web pages inside the application. It is used to turn the application into a web application. public...
Read more >
Android - WebView - Tutorialspoint
WebView is a view that display web pages inside your application. You can also specify HTML string and can show it inside your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found