iOS 获取User-Agent
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS 获取User-Agent相关的知识,希望对你有一定的参考价值。
第一种方法
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString *userAgent = [webView stringByEvaluatingjavascriptFromString:@"navigator.userAgent"];
NSLog(@"-----------------%@",userAgent);
第二种方法
@interface ViewController ()<UIWebViewDelegate>
{
UIWebView *_webView;
NSString *_userAgent;
}
{
_webView = [[UIWebView alloc] init];
_webView.delegate = self;
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
NSLog(@"-----------------%@", [self userAgentString]);
}
-(NSString *)userAgentString {
while (_userAgent == nil)
{
NSLog(@"%@", @"in while");
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
return _userAgent;
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (webView == _webView) {
_userAgent = [request valueForHTTPHeaderField:@"User-Agent"];
// Return no, we don‘t care about executing an actual request.
return NO;
}
return YES;
}
以上是关于iOS 获取User-Agent的主要内容,如果未能解决你的问题,请参考以下文章