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;
        
    }

  • 相关阅读:
    【C语言】学习笔记9——结构struct(2)
    WPF dev 获取gridControl筛选后的数据
    WPF DEV dxc:ChartControl 柱状图
    WPF 重写ListBox(透明效果)
    WPF 后台重写 DataTemplate
    WPF 去掉Drag a column header here to group by that column
    c# 对DataTable进行分组group by
    c# ref与out用法
    WPF canvas设置旋转角度和偏移位置
    WPF 流加载
  • 原文地址:https://www.cnblogs.com/yujidewu/p/5885204.html
Copyright © 2011-2022 走看看