如果你需要获取Android手机的UA信息,请移步这里Android获得UA信息的方法
iPhone中取到UA信息的方法如下:
1.利用浏览器控件UIWebView建立一个http请求
2.在请求建立的期间, 建立一个新的事件循环用来判断UA信息是否已经建立
3.在发送http请求之前截获UA信息, 并且取消这一http请求 ;)
获取iPhone UA的代码如下:
- 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];
- }
程序在iPhone4.1模拟器中运行结果如下:
2010-12-21 12:52:38.302 UATest[6722:207] in while
2010-12-21 12:52:38.305 UATest[6722:207] in while
2010-12-21 12:52:38.307 UATest[6722:207] in while
2010-12-21 12:52:38.307 UATest[6722:207] in while
2010-12-21 12:52:38.308 UATest[6722:207] in while
2010-12-21 12:52:38.309 UATest[6722:207] in while
2010-12-21 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