简体   繁体   English

如何在react-native中为webView请求设置自定义标头

[英]How to set a custom header for webView requests in react-native

I want to be able to detect on my ruby on rails server side that a http request has come from a webView component in my app. 我希望能够在rails服务器端的ruby上检测到http请求来自我的应用程序中的webView组件。 The app is using react-native. 该应用程序正在使用react-native。 In particular, I want to distinguish between a request from the app and a request from mobile safari running on an ios device. 特别是,我想区分来自应用程序的请求和运行在ios设备上的移动safari的请求。

I have tried setting the following in the AppDelegate.m file 我已经尝试在AppDelegate.m文件中设置以下内容

  NSURL *url = [NSURL URLWithString:@"http://localhost:3000/signin"];
  NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
  [request addValue:@"GolfMentor" forHTTPHeaderField:@"User-Agent"];
  [request addValue:@"GolfMentor" forHTTPHeaderField:@"X-MY-CUSTOM-HEADER"];
  NSLog(@"%@", [request allHTTPHeaderFields]);

and the output in the console is 并且控制台中的输出是

GolfMentor[63924:12437749] {
"User-Agent" = GolfMentor;
"X-MY-CUSTOM-HEADER" = GolfMentor;
}

So the customer header appears to be set. 因此,客户标题似乎已设置。 However, when I view the request header in my ruby on rails app, there is no sign of the custom header. 但是,当我在rails app上的ruby中查看请求标头时,没有自定义标头的迹象。 It looks like my changes are being overwritten latter in the execution of the react-native app. 在执行react-native应用程序时,看起来我的更改被覆盖了。 So where and how should I set the custom header? 那么我应该在哪里以及如何设置自定义标头? I am also interested in do the same thing for an android version of the app? 我也有兴趣为Android版本的应用做同样的事情?

Try adding the following lines to AppDelegate.m to set the custom user-agent 尝试将以下行添加到AppDelegate.m以设置自定义用户代理

NSString *newAgent = @"GolfMentor";
NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:newAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];

A fuller answer and the solution for android can be found here . 可以在这里找到更全面的答案和Android的解决方案。

I kind of doubt you will be able to do this. 我有点怀疑你能做到这一点。 If it is not exposed as a prop you can set on the WebView component React Native provides, then I don't think you can do it. 如果它没有作为prop公开,你可以在React Native提供的WebView组件上设置,那么我认为你不能这样做。

As an aside, the WebView component in React Native is not that awesome. 另外,React Native中的WebView组件并不是那么棒。 On the iOS side it wraps UIWebView, but for iOS 8+ there is WkWebView, which is more standards-compliant and considerably faster than UIWebView. 在iOS方面它包装了UIWebView,但是对于iOS 8+,有WkWebView,它更符合标准并且比UIWebView快得多。

http://blog.initlabs.com/post/100113463211/wkwebview-vs-uiwebview http://blog.initlabs.com/post/100113463211/wkwebview-vs-uiwebview

I am currently working on an app that makes extensive use of webviews, and we wound up writing our own native components for iOS and Android. 我目前正在开发一个广泛使用webview的应用程序,我们最终编写了适用于iOS和Android的本机组件。 This is easier than it sounds, especially if you already have native development experience. 这比听起来容易,特别是如果您已经拥有本机开发经验。 So if you're doing a lot with webviews, and the out-of-the-box component doesn't give you what you want (and you want the added benefits of WkWebView vs. UIWebView), I would consider rolling your own. 因此,如果您使用Web视图做了很多事情,并且开箱即用的组件没有提供您想要的内容(并且您希望获得WkWebView与UIWebView的额外好处),我会考虑自己编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM