zoukankan      html  css  js  c++  java
  • iOS开发之Objective-C与JavaScript的交互

       UIWebView是iOS最常用的SDK之一,它有一个stringByEvaluatingJavaScriptFromString方法可以将javascript嵌入页面中,通过这个方法我们可以在iOS中与UIWebView中的网页元素交互。

    stringByEvaluatingJavaScriptFromString

        使用stringByEvaluatingJavaScriptFromString方法,需要等UIWebView中的页面加载完成之后去调用。我们在界面上拖放一个UIWebView控件。在Load中将google mobile加载到这个控件中,代码如下:

    复制代码
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        webview.backgroundColor = [UIColor clearColor];  
        webview.scalesPageToFit =YES;
        webview.delegate =self;
        NSURL *url =[[NSURL alloc] initWithString:@"http://www.google.com.hk/m?gl=CN&hl=zh_CN&source=ihp"];
    
        NSURLRequest *request =  [[NSURLRequest alloc] initWithURL:url];
        [webview loadRequest:request];   
    }
    复制代码

    我们在webViewDidFinishLoad方法中就可以通过javascript操作界面元素了。

    1、获取当前页面的url。

    - (void)webViewDidFinishLoad:(UIWebView *)webView {  
      NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
    }

    2、获取页面title:

    - (void)webViewDidFinishLoad:(UIWebView *)webView {  
       NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
    
       NSString *title = [webview stringByEvaluatingJavaScriptFromString:@"document.title"]; 
    }

    3、修改界面元素的值。

        NSString *js_result = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('q')[0].value='朱祁林';"];

    4、表单提交:

            NSString *js_result2 = [webView stringByEvaluatingJavaScriptFromString:@"document.forms[0].submit(); "];

    这样就实现了在google搜索关键字:“朱祁林”的功能。

    5、插入js代码

    上面的功能我们可以封装到一个js函数中,将这个函数插入到页面上执行,代码如下:

    复制代码
             [webView stringByEvaluatingJavaScriptFromString:@"var script = document.createElement('script');"  
             "script.type = 'text/javascript';"  
             "script.text = "function myFunction() { "  
             "var field = document.getElementsByName('q')[0];"  
             "field.value='朱祁林';"  
             "document.forms[0].submit();"  
             "}";"  
             "document.getElementsByTagName('head')[0].appendChild(script);"];   
            
            [webView stringByEvaluatingJavaScriptFromString:@"myFunction();"];  
    复制代码

    看上面的代码:

    a、首先通过js创建一个script的标签,type为'text/javascript'。

    b、然后在这个标签中插入一段字符串,这段字符串就是一个函数:myFunction,这个函数实现google自动搜索关键字的功能。

    c、然后使用stringByEvaluatingJavaScriptFromString执行myFunction函数。

    演示:

    第一步打开google mobile网站

    第二步输入关键字


    第三步搜素

    总结:这篇文章主要是讲解了stringByEvaluatingJavaScriptFromString的用法,它的功能非常的强大,用起来非常简单,通过它我们可以很方便的操作uiwebview中的页面元素。

  • 相关阅读:
    基于策略梯度的强化学习论文调研
    Soft Actor-Critic: Off-Policy Maximum Entropy Deep Reinforcement Learning with a Stochastic Actor
    固定随机种子比较强化学习算法
    Action and learning shape the activity of neuronal circuits in the visual cortex
    大脑中的记忆机制
    A Simple Neural Attentive Meta-Learner
    【Error】Creating Server TCP listening socket *:6379: bind: No such file or directory
    Redis安装配置
    MySQL中文入库问题
    Nginx启动/重启失败
  • 原文地址:https://www.cnblogs.com/melons/p/5792004.html
Copyright © 2011-2022 走看看