IOS UIWebView Modify User-agent

Source: Internet
Author: User
Tags vars

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
    1. Nsmutableurlrequest *request = [[Nsmutableurlrequest alloc] Initwithurl:url];
    2. [Request AddValue:@ "jiecao/2.4.7" Forhttpheaderfield:@ "User-agent"];
    3. [self. WebView Loadrequest:request];

Invalid!!!


Scenario Two:

[OBJC]View Plaincopy
    1. #pragma  mark -  webview delegate  
    2.   
    3. -  ( bool) WebView: (uiwebview *) webview  Shouldstartloadwithrequest: (nsurlrequest *) request  Navigationtype: (Uiwebviewnavigationtype) navigationtype {  
    4.     nsmutableurlrequest *req =  (nsmutableurlrequest *) request;  
    5.      [req addvalue:@ "jiecao/2.4.7"   Forhttpheaderfield:@ "User-agent"];  
    6. }   


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
  1. Get the original user-agent of WebView
  2. UIWebView *webview = [[UIWebView alloc] Initwithframe:cgrectzero];
  3. NSString *oldagent = [WebView stringbyevaluatingjavascriptfromstring:@ "Navigator.useragent"];
  4. NSLog (@ "Old agent:%@", oldagent);
  5. add my info to the new agent
  6. NSString *newagent = [oldagent stringbyappendingstring:@ "jiecao/2.4.7 Ch_appstore"];
  7. NSLog (@ "New agent:%@", newagent);
  8. Regist the new agent
  9. Nsdictionary *dictionnary = [[Nsdictionary alloc] initwithobjectsandkeys:newagent, @ "useragent", Nil  Nil];
  10. [[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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.