Android – how to set custom headers for WebView requests in react native
I want to be able to detect that HTTP requests come from the WebView component in my application on rails server-side ruby. The application is using the native. In particular, I want to distinguish between requests from applications and requests from mobile Safari running on IOS devices
I tried to set the following in the appdelegate. M file
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]);
The output in the console is
GolfMentor[63924:12437749] {
"User-Agent" = GolfMentor;
"X-MY-CUSTOM-HEADER" = GolfMentor;
}
Therefore, the customer header seems to have been set. However, when I view the request header in ruby of the rails application, there is no sign of customizing the header. It seems that my changes were later overwritten in the execution of the react native application. So where and how should I set the custom header? Am I interested in doing the same for the Android version of the app?
resolvent:
Try adding the following line to appdelegate. M to set up a custom user agent
NSString *newAgent = @"GolfMentor";
NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:newAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
You can find more complete answers and solutions for Android here