Skip to content
This repository has been archived by the owner on Jul 2, 2022. It is now read-only.

Send correct User-Agent header #10

Closed
jupasources opened this issue Dec 3, 2020 · 16 comments
Closed

Send correct User-Agent header #10

jupasources opened this issue Dec 3, 2020 · 16 comments
Assignees
Labels
enhancement New feature or request

Comments

@jupasources
Copy link

Hello,
Using this plugin all requests comes with native iOS User-Agent header.

The header looks similar to this: "appName/3.0.10 CFNetwork/978.0.7 Darwin/18.7.0"
Expected value should be like this: "Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Mobile/15E148 Safari/604.1"

I see that all ajax calls send correct header, but in backend we get that iOS specific one. This is currently a problem to Authenticate with Microsoft Azure where conditional access policy enabled to check the device model. It simply can't recognize the device.

Could that be fixed?

Thank you!

@globules-io globules-io added the incomplete bug report Preferences and/or version information missing label Dec 3, 2020
@jupasources
Copy link
Author

Sorry for not being precise.
The header values I mentioned is for reference only (examples). I'm not talking about changing the header. The problem is that when you execute an ajax call from the app it should send the User-Agent header as it set by default where we have information about browser and device. The plugin intercepts the request and make it as native request where the User-Agent header is overwritten by some native ids like this "appName/3.0.10 CFNetwork/978.0.7 Darwin/18.7.0". When backend receives the request this User-Agent string makes no sense at all. So the expected behavior is to send User-Agent header one to one as it set by default in Cordova webview.

@jupasources
Copy link
Author

Cordova has a preference "OverrideUserAgent".
So if I set something like this in config.xml:
<preference name="OverrideUserAgent" value="my_cool_browser" />
I would expect to receive "my_cool_browser" in User-Agent header on server side.

@globules-io
Copy link
Owner

Ok gotcha, I'll look into it

@jupasources
Copy link
Author

Thank you! This will be a lifesaver for us.

@globules-io
Copy link
Owner

globules-io commented Dec 3, 2020

I believe I need to add something like this, can't test right now.. If you want to clone add/test these lines in CDVWKWebViewFileXhr.m after line 88 ? Otherwise this can take a few hours here

//custom user agent
value = [self.commandDelegate.settings cordovaSettingForKey:@"OverrideUserAgent"];
if (value != nil) {
    NSLog(@"INFO: Custom User-Agent");
    [wkWebView.configuration.preferences setValue:value forKey:@"customUserAgent"];
}

@globules-io globules-io added enhancement New feature or request and removed incomplete bug report Preferences and/or version information missing labels Dec 3, 2020
@jupasources
Copy link
Author

jupasources commented Dec 3, 2020

I'm getting the error:
.../Plugins/@globules-io/cordova-plugin-ios-xhr/CDVWKWebViewFileXhr.m:91:44: No visible @interface for 'NSDictionary' declares the selector 'cordovaSettingForKey:'

and then I tried to hardcode custom string, but new error:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<WKPreferences 0x280598100> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key customUserAgent.'

I'm not familiar with iOS native development so can't really help here.

@globules-io
Copy link
Owner

Sorry, meant to be

value = [self.commandDelegate.settings cdvwkStringForKey:@"OverrideUserAgent"];
if (value != nil) {
    NSLog(@"INFO: Custom User-Agent");
    [wkWebView.configuration.preferences setValue:value forKey:@"customUserAgent"];
}

cdvwkStringForKey vs cordovaSettingForKey

@jupasources
Copy link
Author

jupasources commented Dec 3, 2020

Now it's building fine. But User-Agent header is still being overwritten. This code most likely set the header on webview level and not on iOS native level. We need to set it on native level or set it with each http requests.

@globules-io
Copy link
Owner

Ok I'd have to spend some time on it, can't do it right now

@jupasources
Copy link
Author

I have made it working by adding following code in line 385:

dispatch_sync(dispatch_get_main_queue(), ^{
        [request setValue:weakWebView.customUserAgent forHTTPHeaderField:@"User-Agent"];
});

Not sure about weakWebView.customUserAgent I wasn't able to get it from wkWebView.configuration.preferences or maybe it's same.
I'm also using plugin cordova-plugin-useragent to set the customUserAgent property of webview to make it working.
UserAgent.set(navigator.userAgent, function(){console.log("User Agent set to: " + navigator.userAgent)});

With that combination I get expected results.

@jupasources
Copy link
Author

It would be cool to avoid usage of cordova-plugin-useragent and combine the code to get navigator.userAgent and set it to User-Agent header of the request.

@globules-io
Copy link
Owner

Please test 1.1.0 using CustomUserAgent flag (see updated readme)

@jupasources
Copy link
Author

It works fine with that flag. Thanks! But then the User-Agent is all the time same for all devices (hardcoded).
The perfect solution would be if CustomUserAgent flag is not set than use default value from webview (navigator.userAgent) otherwise server is not able to recognize device model and os version.

@jupasources
Copy link
Author

jupasources commented Dec 3, 2020

or event better to introduce one more boolean flag "UseDefaultUserAgent" or "UseWebViewUserAgent". If set to true than use the value from navigator.userAgent. This approach will keep existing behavior but introduce the option we are looking for. I strongly believe this could help others as well.

@globules-io
Copy link
Owner

No can't do that because other plugins/objects can rely on the user agent to catch if it is running on iOS/Android etc. Also this is a XHR plugin so I will keep it related to XHR requests without tempering with the webview too much. This is as far as I will take it but feel free to fork and make your own modifications.

@jupasources
Copy link
Author

Ok, I understand.
Thank you anyways for quick responses and reaction!

Repository owner locked and limited conversation to collaborators Feb 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants