zoukankan      html  css  js  c++  java
  • 利用UIWebView获取userAgent需要注意的地方

    网络通信有时候需要传递参数userAgent,iOS中获取userAgent很简单.

        UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectZero];
        NSString *userAgentString = [webView stringByEvaluatingJavaScriptFromString:
                                     @"navigator.userAgent"];

    打印信息如下

    userAgentString --> Mozilla/5.0 (iPod touch; CPU iPhone OS 7_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D167

    有时候,你想在后台线程中获取userAgent,直接就有如下信息提示:

    bool _WebTryThreadLock(bool), 0x14e6b590: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...

    找了一下资料,原来是这么回事:

    Webviews and the Webkit JavaScript engine are both single-threaded and cannot be used on a background thread.

    Webview以及Webkit Java脚本引擎,这两个都是单线程的,不能够在子线程中使用.

    所以,不要试图在子线程中获取userAgent,虽然获取userAgent耗时间,但第一次获取后可以把userAgent值存储起来,下回直接读取就行了.

    附录:

    stringByEvaluatingJavaScriptFromString使用方法
     
    使用stringByEvaluatingJavaScriptFromString方法,需要等UIWebView中的页面加载完成之后去调用。我们在界面上拖放一个UIWebView控件。在Load中将googlemobile加载到这个控件中,代码如下:
    1. - (void)viewDidLoad
    2. {
    3. [super viewDidLoad];
    4. webview.backgroundColor = [UIColor clearColor];
    5. webview.scalesPageToFit =YES;
    6. webview.delegate =self;
    7. NSURL *url =[[NSURL alloc] initWithString:@"http://www.google.com.hk/m?gl=CN&hl=zh_CN&source=ihp"];
    8.
    9. NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    10. [webview loadRequest:request];
    11. }
    我们在webViewDidFinishLoad方法中就可以通过javascript操作界面元素了。
    1、获取当前页面的url。
    1. - (void)webViewDidFinishLoad:(UIWebView *)webView {
    2. NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
    3. }
    2、获取页面title:
    1. - (void)webViewDidFinishLoad:(UIWebView *)webView {
    2. NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
    3.
    4. NSString *title = [webview stringByEvaluatingJavaScriptFromString:@"document.title"];
    5. }
    3、修改界面元素的值。
    1. NSString *js_result = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('q')[0].value='朱祁林';"];
    4、表单提交:
    1. NSString *js_result2 = [webView stringByEvaluatingJavaScriptFromString:@"document.forms[0].submit(); "];
    这样就实现了在google搜索关键字:“朱祁林”的功能。
    5、插入js代码
    上面的功能我们可以封装到一个js函数中,将这个函数插入到页面上执行,代码如下:
    1. [webView stringByEvaluatingJavaScriptFromString:@"var script = document.createElement('script');"
    2. "script.type = 'text/javascript';"
    3. "script.text = "function myFunction() { "
    4. "var field = document.getElementsByName('q')[0];"
    5. "field.value='朱祁林';"
    6. "document.forms[0].submit();"
    7. "}";"
    8. "document.getElementsByTagName('head')[0].appendChild(script);"];
    9.
    10. [webView stringByEvaluatingJavaScriptFromString:@"myFunction();"];
    看上面的代码:
    a、首先通过js创建一个script的标签,type为'text/javascript'。
    b、然后在这个标签中插入一段字符串,这段字符串就是一个函数:myFunction,这个函数实现google自动搜索关键字的功能。
    c、然后使用stringByEvaluatingJavaScriptFromString执行myFunction函数。
    演示:
    第一步打开google mobile网站
     
    第二步输入关键字
     
    第三步搜素
     
    总结:这篇文章主要是讲解了stringByEvaluatingJavaScriptFromString的用法,它的功能非常的强大,用起来非常简单,通过它我们可以很方便的操作uiwebview中的页面元素。
  • 相关阅读:
    字典序 动物统计 输出姓名和个数
    心急的C小加
    docker rabbitmq
    Docker Device Mapper 使用 direct-lvm
    FW docker使用问题总结,解决国内不能访问gcr.io的问题
    巧用AWK处理二进制数据文件
    Webshell清除-解决驱动级文件隐藏挂马
    phantomflow phantomcss, phantomas
    cobbler, puppet
    java javassis crack class
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3668320.html
Copyright © 2011-2022 走看看