强制React-Native webview将url加载为Mobile?

问题描述 投票:0回答:2

在java with android studio中我通过添加以下代码来实现:

webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);

webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);

webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);

但我不知道如何在React-Native中做到这一点,我尝试了一些css风格,但没有做到这一点。

这是我在React中的webview:

<WebView
  ref="WebView"
  scalesPageToFit={true}
  onLoadStart={this.onLoadStart}
  onLoadEnd={this.onLoadEnd}
  source = {{ uri:this.props.navigation.getParam('main_url', 
  'https://xxxx.xxx/')}}
  style={{backgroundColor: 'transparent',width: '100%'}}
/>:null}
java android react-native android-webview
2个回答
0
投票

使用用户代理。

注意:这仅适用于Android。对于iOS,请使用此链接Change User-Agent

<Webview
  userAgent={"Mozilla/5.0 (Linux; Android 8.0.0; Pixel 2 XL Build/OPD1.170816.004) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3714.0 Mobile Safari/537.36"
/>

0
投票

受到Sanyam评论的启发,您需要设置用户代理才能将其加载为移动设备:

<WebView
    userAgent={DeviceInfo.getUserAgent() + " - MYAPPNAME - android "}
    {...otherProps}
/>
© www.soinside.com 2019 - 2024. All rights reserved.