Search code examples
javaandroidreact-nativeandroid-webview

Force React-Native webview to load url as Mobile?


In java with android studio I achieve this by adding this code:

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);

but I don't know how to do this in React-Native, I try some css style but nothing did the trick.

here is my webview in React:

<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}

Solution

  • Use user-agent.

    Note: This used to only work in Android. For iOS, you could use this link Change User-Agent Now it seems to work on both iOS and android

    <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"
    />