获取UA的方法流程就是
- 利用浏览器控件UIWebView建立一个http请求
- 在请求建立的期间, 建立一个新的事件循环用来判断UA信息是否已经建立
- 在发送http请求之前截获UA信息, 并且取消这一http请求
/**
获取UA 下边三个方法都是
@return UA
*/
- (NSString *)createHttpRequest {
self.webView = [[UIWebView alloc] init];
self.webView.delegate = self;
[self.webView loadRequest:[NSURLRequest requestWithURL:
[NSURL URLWithString:@"http://www.eoe.cn"]]];
NSLog(@"%@", [self userAgentString]);
return [self userAgentString];
}
-(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;
}
网友评论