首页 > 解决方案 > Webview userAgent 在反应 natvie 中不起作用

问题描述

<WebView source={{html:'...'}} userAgent={DeviceInfo.getUserAgent()}></WebView>

我用来WebView向服务器发送请求并设置userAgent. 但是发现user-agent服务器收到的是okhttp/3.6.0. 那么有什么问题userAgent呢?

标签: react-nativewebviewuser-agent

解决方案


您发送了收到的数据DeviceInfo.getUserAgent(),我建议将您的自定义数据添加userAgent到您打算在服务器上传递的属性中。例如在 WebView 中设置自定义数据,它应该在 Android 版本中按预期工作。

 <WebView
      source={ {uri: this.props.url} }
      userAgent="demo-react-native-app"
    />

最终它不适用于 iOS 版本,这里 AppDelegate.m 文件中的几行代码需要它的支持。

NSString *newAgent = @"demo-react-native-app";
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:newAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

推荐阅读