IOS UIWebView Custom UserAgent

Source: Internet
Author: User

My demand is: Users use my browser to browse the Web, the server can get my browser name and version number, webmaster tools can see the client source. OK, the solution is to modify the system default User agent UserAgent, this is not bad.

Once did the app also has such demand, the idea is in UIWebView request Header set useragent, finally confirmed this idea is not feasible, later because of time problem, this function gave up. Now there is an app and this demand, see UC Browser settings have a UA settings, let me is very envious, really willing to solve the problem of people always lucky, and then find related articles online. Learn about two ideas, the first is to use a private API, and the second is within the iOS SDK. The first kind of here to give up not to speak, only on the second realization of ideas.

Get IOS default useragent, you can cleverly create an empty UIWebView to get:

NSString *useragent = [[[[UIWebView alloc] init] stringbyevaluatingjavascriptfromstring:@ "navigator.useragent"];

Get the app name, my app has localized support, so it's written as follows

NSString *appname = nslocalizedstringfromtable (@ "Cfbundledisplayname", @ "Infoplist", nil);

If you don't need a localized app name, you can use the following sentence

NSString * AppName = [[NSBundle mainbundle] infodictionary][@ "Cfbundledisplayname"];

NSString *version = [[NSBundle mainbundle] infodictionary][@ "cfbundleshortversionstring"];

NSString *customuseragent = [useragent stringbyappendingformat:@ "%@/%@", AppName, Version];

[[Nsuserdefaults Standarduserdefaults] registerdefaults:@{@ "useragent": customuseragent}];

----------Write a test code, remember to set delegate Oh, this is just the test code

UIWebView *webview = [[UIWebView alloc] init];

Webview.delegate = self;

[WebView loadrequest:[nsurlrequest requestwithurl:[nsurlurlwithstring:@ "http://www.baidu.com/"]];

-(void) Webviewdidfinishload: (UIWebView *) WebView

{

NSLog (@ "useragent =%@", [WebView stringbyevaluatingjavascriptfromstring:@ "navigator.useragent"]);

}

The results from the Xcode 5.1.1 IOS 7.1 simulator are:

mozilla/5.0 (IPhone; CPU iPhone os 7_1 like Mac os X applewebkit/537.51.2 (khtml, like Gecko) Chinese browser/1.2.2

IOS UIWebView Custom UserAgent

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.