Last active
April 13, 2020 14:00
-
-
Save shiv19/f9f6895bb9ddf7c93af69b2bbeb3c7ba to your computer and use it in GitHub Desktop.
NativeScript Disable Zoom on Webview, and load hyperlinks on default browser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
What is dependency for this? What core modules I have to include to make this work? Thnaks
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());
}
}
}
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
Is this only for Android?