首页 > 移动平台 > 详细

iOS 获取User-Agent

时间:2016-09-19 15:59:33      阅读:374      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!