If you need to obtain the UA information of the Android phone, go here to Android to obtain the UA Information
The method for obtaining UA information in iPhone is as follows:
1. Use the browser control UIWebView to create an http request
2. During the creation of the request, a new event loop is created to determine whether the UA information has been created.
3. the UA information is intercepted before an http request is sent and the http request is canceled ;)
The code for getting the iPhone UA is as follows:
- UIWebView *_webView;
- NSString *userAgent;
-
- - (void)createHttpRequest {
- _webView = [[UIWebView alloc] init];
- _webView.delegate = self;
- [_webView loadRequest:[NSURLRequest requestWithURL:
- [NSURL URLWithString:@"http://www.eoe.cn"]]];
- NSLog(@"%@", [self userAgentString]);
- [_webView release];
- }
-
- -(NSString *)userAgentString
- {
- while (self.userAgent == nil)
- {
- NSLog(@"%@", @"in while");
- [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
- }
- return self.userAgent;
- }
-
- -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
- {
- if (webView == _webView) {
- self.userAgent = [request valueForHTTPHeaderField:@"User-Agent"];
- // Return no, we don't care about executing an actual request.
- return NO;
- }
- return YES;
- }
-
- - (void)dealloc {
- [userAgent release];
- [super dealloc];
- }
-
The program runs in the iPhone4.1 simulator as follows:
12:52:38. 302 UATest [6722: 207] in while
12:52:38. 305 UATest [6722: 207] in while
12:52:38. 307 UATest [6722: 207] in while
12:52:38. 307 UATest [6722: 207] in while
12:52:38. 308 UATest [6722: 207] in while
12:52:38. 309 UATest [6722: 207] in while
12:52:38. 312 UATest [6722: 207] Mozilla/5.0 (iPhone Simulator; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) mobile/8B117