码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 获取User-Agent

时间:2016-09-19 16:01:40      阅读:360      评论:0      收藏:0      [点我收藏+]

标签:

第一种方法

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

标签:

原文地址:http://www.cnblogs.com/yujidewu/p/5885204.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!