WebView does not provide an interface for setting up user-agent, whether it is setting the request to load or setting the request in delegate, the test is not valid. As follows:
Programme one:
[OBJC]View Plaincopy
- Nsmutableurlrequest *request = [[Nsmutableurlrequest alloc] Initwithurl:url];
- [Request AddValue:@ "jiecao/2.4.7" Forhttpheaderfield:@ "User-agent"];
- [self. WebView Loadrequest:request];
Invalid!!!
Scenario Two:
[OBJC]View Plaincopy
- #pragma mark - webview delegate
-
- - ( bool) WebView: (uiwebview *) webview Shouldstartloadwithrequest: (nsurlrequest *) request Navigationtype: (Uiwebviewnavigationtype) navigationtype {
- nsmutableurlrequest *req = (nsmutableurlrequest *) request;
- [req addvalue:@ "jiecao/2.4.7" Forhttpheaderfield:@ "User-agent"];  
- }
Invalid!!!
My requirement is not only to be able to change user-agent, but also to keep WebView the original user-agent information, that is, I need to append information on it. The answers are collected on StackOverflow and the final summary of the solutions is as follows:
At startup, for example, add the following code to the Appdelegate:
[OBJC]View Plaincopy
- Get the original user-agent of WebView
- UIWebView *webview = [[UIWebView alloc] Initwithframe:cgrectzero];
- NSString *oldagent = [WebView stringbyevaluatingjavascriptfromstring:@ "Navigator.useragent"];
- NSLog (@ "Old agent:%@", oldagent);
- add my info to the new agent
- NSString *newagent = [oldagent stringbyappendingstring:@ "jiecao/2.4.7 Ch_appstore"];
- NSLog (@ "New agent:%@", newagent);
- Regist the new agent
- Nsdictionary *dictionnary = [[Nsdictionary alloc] initwithobjectsandkeys:newagent, @ "useragent", Nil Nil];
- [[Nsuserdefaults Standarduserdefaults] registerdefaults:dictionnary];
In this way, WebView at the request of the user-agent is we set this, if you need to change the user-agent in webview use process, you need to modify the user-agent in this manner, and then re-instantiate a webview.
Reference:
http://stackoverflow.com/questions/478387/change-user-agent-in-uiwebview-iphone-sdk/23654363#23654363
IOS UIWebView Modify User-agent