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