Skip to content

Instantly share code, notes, and snippets.

@shiv19
Last active April 13, 2020 14:00
Show Gist options
  • Save shiv19/f9f6895bb9ddf7c93af69b2bbeb3c7ba to your computer and use it in GitHub Desktop.
Save shiv19/f9f6895bb9ddf7c93af69b2bbeb3c7ba to your computer and use it in GitHub Desktop.
NativeScript Disable Zoom on Webview, and load hyperlinks on default browser
// Lets you use webcam and other mobile hardware inside NativeScript WebView
exports.webViewLoaded = function (args) {
var webview = args.object;
var TNSWebViewClient =
android.webkit.WebViewClient.extend({
shouldOverrideUrlLoading: function (view, url) {
if (url != null && url.startsWith("http://")) {
console.log(url);
// use openUrl form utils module to open the page in a browser
return true;
} else {
return false;
}
}
});
var TNSWebChromeClient =
android.webkit.WebChromeClient.extend({
onPermissionRequest: function (request) {
request.grant(request.getResources());
}
});
if (isAndroid) {
webview.android.getSettings().setDisplayZoomControls(false);
webview.android.getSettings().setBuiltInZoomControls(false);
webview.android.getSettings().setAllowFileAccessFromFileURLs(true);
webview.android.getSettings().setAllowUniversalAccessFromFileURLs(true);
webview.android.getSettings().setMediaPlaybackRequiresUserGesture(false);
webview.android.getSettings().setUseWideViewPort(true);
webview.android.getSettings().setDomStorageEnabled(true);
webview.android.setWebViewClient(new TNSWebViewClient());
webview.android.setWebChromeClient(new TNSWebChromeClient());
}
}
exports.webViewLoaded = function(args) {
var webview = args.object;
var TNSWebViewClient =
android.webkit.WebViewClient.extend({
shouldOverrideUrlLoading: function(view, url) {
if (url != null && url.startsWith("http://")) {
console.log(url);
// use openUrl form utils module to open the page in a browser
return true;
} else {
return false;
}
}
});
if (isAndroid) {
webview.android.getSettings().setDisplayZoomControls(false);
webview.android.getSettings().setBuiltInZoomControls(false);
webview.android.setWebViewClient(new TNSWebViewClient());
}
}
@erikandershed
Copy link

Is this only for Android?

@markosole
Copy link

What is dependency for this? What core modules I have to include to make this work? Thnaks

@b4rtt
Copy link

b4rtt commented Jul 23, 2018

angular there

import { isAndroid } from 'platform';
declare var android: any;

@Component()

public class YourComponent ... {
    public webViewLoaded(args) {
        const webview = args.object;
        const TNSWebViewClient =
            android.webkit.WebViewClient.extend({
                shouldOverrideUrlLoading: function(view, url) {
                    if (url != null && url.startsWith("http://")) {
                        console.log(url);
                        // use openUrl form utils module to open the page in a browser
                        return true;
                    } else {
                        return false;
                    }
                }
            });
        if (isAndroid) {
            webview.android.getSettings().setDisplayZoomControls(false);
            webview.android.getSettings().setBuiltInZoomControls(false);
            webview.android.setWebViewClient(new TNSWebViewClient());
        } 
    }
}

@timdoege
Copy link

On Android 21+ the URL needs to be used as openUrl(url.getUrl().toString());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment