I have a business need to be able to customize the UserAgent for an embedded UIWebView. (For instance, I'd like the server to respond differently if, say, a user is using one version of the app versus another.)
Is it possible to customize the UserAgent in the existing iPhone SDK's UIWebView control the way it is, say, for an embedded IE browser in a Windows app?
Very simple in Swift. Just place the following into your App Delegate.
If you want to append to the existing agent string then:
Note: You may will need to uninstall and reinstall the App to avoid appending to the existing agent string.
Using @"User_Agent" simply causes a custom header to appear in the GET request.
The above is what appears in the dissected HTTP packet, essentially confirming what Sfjava was quoting from that forum. It's interesting to note that "User-Agent" gets turned into "User_agent."
Try this in the AppDelegate.m
To just add a custom content to the current UserAgent value, do the following:
1 - Get the user agent value from a NEW WEBVIEW
2 - Append the custom content to it
3 - Save the new value in a dictionary with the key UserAgent
4 - Save the dictionary in standardUserDefaults.
See the exemple below:
I had this problem too, and tried all methods. I found that only this method works (iOS 5.x): UIWebView iOS5 changing user-agent
The principle is to set the user agent permanently in the user settings. This works; Webview sends the given header. Just two lines of code:
Setting User-Agent, or User_Agent in the mutable request, or overriding the setValue in the NSHttpRequest by swizzling, - I tried all that and controlled the results with wireshark, and none of that seems to work, because Webview still uses the user agent value from the user defaults, no matter what you try to set in the NSHttpRequest.
This solution seems to have been seen as a pretty clever way to do it
changing-the-headers-for-uiwebkit-http-requests
It uses Method Swizzling and you can learn more about it on the CocoaDev page
Give it a look !