zoukankan      html  css  js  c++  java
  • 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;
        
    }

  • 相关阅读:
    js小数点失精算法修正
    ActiveX控件之ActiveXObject is not defined
    js通过日期计算属于星期几
    标准日期格式化
    js阿拉伯数字转中文大写
    RPC 原理的前生今世
    大型网站架构系列:20本技术书籍推荐
    Zookeeper核心机制
    建造者模式
    模板方法模式
  • 原文地址:https://www.cnblogs.com/yujidewu/p/5885204.html
Copyright © 2011-2022 走看看