如何在 Nativescript IOS Webview 上更改用户代理或设置为桌面模式
How to change User Agent or set to Desktop Mode on Nativescript IOS Webview
我想强制 Webview 在 Nativescript IOS Webview 上将其设置为桌面模式,目前我通过覆盖 Webview class:[=12 在 Android 上工作=]
/**
* Initializes webViewInterface for communication between webview and android/ios
*/
private setupWebViewInterface() {
let webView: WebView = this.webView.nativeElement;
webView.on(WebView.loadFinishedEvent, (args: LoadEventData) => {
if (isAndroid) {
(<WebView>args.object).android.getSettings().setJavaScriptEnabled(true);
let newUA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36";
(<WebView>args.object).android.getSettings().setUserAgentString(newUA);
(<WebView>args.object).android.setWebChromeClient(new MyWebChromeClient());
}
});
}
但是对于如何在 IOS Webview 上执行此操作有点迷茫,特别是注意到它在幕后使用 Safari。
只需在原生视图(WKWebView)上设置customUserAgent
。
webView.on(WebView.loadFinishedEvent, (args: LoadEventData) => {
if (isAndroid) {
(<WebView>args.object).android.getSettings().setJavaScriptEnabled(true);
let newUA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36";
(<WebView>args.object).android.getSettings().setUserAgentString(newUA);
(<WebView>args.object).android.setWebChromeClient(new MyWebChromeClient());
} else {
(<WebView>args.object).ios.customUserAgent = "Your user agent";
}
});
我想强制 Webview 在 Nativescript IOS Webview 上将其设置为桌面模式,目前我通过覆盖 Webview class:[=12 在 Android 上工作=]
/**
* Initializes webViewInterface for communication between webview and android/ios
*/
private setupWebViewInterface() {
let webView: WebView = this.webView.nativeElement;
webView.on(WebView.loadFinishedEvent, (args: LoadEventData) => {
if (isAndroid) {
(<WebView>args.object).android.getSettings().setJavaScriptEnabled(true);
let newUA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36";
(<WebView>args.object).android.getSettings().setUserAgentString(newUA);
(<WebView>args.object).android.setWebChromeClient(new MyWebChromeClient());
}
});
}
但是对于如何在 IOS Webview 上执行此操作有点迷茫,特别是注意到它在幕后使用 Safari。
只需在原生视图(WKWebView)上设置customUserAgent
。
webView.on(WebView.loadFinishedEvent, (args: LoadEventData) => {
if (isAndroid) {
(<WebView>args.object).android.getSettings().setJavaScriptEnabled(true);
let newUA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36";
(<WebView>args.object).android.getSettings().setUserAgentString(newUA);
(<WebView>args.object).android.setWebChromeClient(new MyWebChromeClient());
} else {
(<WebView>args.object).ios.customUserAgent = "Your user agent";
}
});