How to obtain User-Agent (UA) Information on iPhone

Source: Internet
Author: User

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:

 
 
  1. UIWebView *_webView; 
  2. NSString *userAgent; 
  3.  
  4. - (void)createHttpRequest { 
  5.     _webView = [[UIWebView alloc] init]; 
  6.     _webView.delegate = self; 
  7.     [_webView loadRequest:[NSURLRequest requestWithURL: 
  8.     [NSURL URLWithString:@"http://www.eoe.cn"]]]; 
  9.     NSLog(@"%@", [self userAgentString]); 
  10.     [_webView release]; 
  11.  
  12. -(NSString *)userAgentString 
  13.     while (self.userAgent == nil)  
  14.     { 
  15.         NSLog(@"%@", @"in while"); 
  16.         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; 
  17.         } 
  18.     return self.userAgent; 
  19.  
  20. -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
  21.     if (webView == _webView) { 
  22.     self.userAgent = [request valueForHTTPHeaderField:@"User-Agent"]; 
  23.     // Return no, we don't care about executing an actual request. 
  24.     return NO; 
  25.     return YES; 
  26.  
  27. - (void)dealloc { 
  28. [userAgent release]; 
  29.     [super dealloc]; 
  30.   

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

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.