草庐IT

javascript - 如何在 iOS 中调用 Javascript 方法?

coder 2023-08-11 原文

<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/quiz-1.js"></script>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<p class="question">1. What is the answer to this question?</p>        

<ul class="answers">            
<input type="radio" name="q1" value="a" id="q1a"><label for="q1a">Answer 1</label><br/>          
<input type="radio" name="q1" value="b" id="q1b"><label for="q1b">Answer 2</label><br/>            
<input type="radio" name="q1" value="c" id="q1c"><label for="q1c">Answer 3</label><br/>            
<input type="radio" name="q1" value="d" id="q1d"><label for="q1d">Answer 4</label><br/>       
</ul>        


<p class="question">2. What is the answer to this question?</p>        

<ul class="answers">            
<input type="radio" name="q2" value="a" id="q2a"><label for="q2a">Answer 1</label><br/>           
<input type="radio" name="q2" value="b" id="q2b"><label for="q2b">Answer 2</label><br/>            
<input type="radio" name="q2" value="c" id="q2c"><label for="q2c">Answer 3</label><br/>           
<input type="radio" name="q2" value="d" id="q2d"><label for="q2d">Answer 4</label><br/>       
</ul>        

<p class="question">3. What is the answer to this question?</p>        

<ul class="answers">            
<input type="radio" name="q3" value="a" id="q3a"><label for="q3a">Answer 1</label><br/>            
<input type="radio" name="q3" value="b" id="q3b"><label for="q3b">Answer 2</label><br/>            
<input type="radio" name="q3" value="c" id="q3c"><label for="q3c">Answer 3</label><br/>           
<input type="radio" name="q3" value="d" id="q3d"><label for="q3d">Answer 4</label><br/>       
</ul>        

<p class="question">4. What is the answer to this question?</p>        

<ul class="answers">           
<input type="radio" name="q4" value="a" id="q4a"><label for="q4a">Answer 1</label><br/>            
<input type="radio" name="q4" value="b" id="q4b"><label for="q4b">Answer 2</label><br/>            
<input type="radio" name="q4" value="c" id="q4c"><label for="q4c">Answer 3</label><br/>            
<input type="radio" name="q4" value="d" id="q4d"><label for="q4d">Answer 4</label><br/>        
</ul>        

<p class="question">5. What is the answer to this question?</p>        

<ul class="answers">            
<input type="radio" name="q5" value="a" id="q5a"><label for="q5a">Answer 1</label><br/>            
<input type="radio" name="q5" value="b" id="q5b"><label for="q5b">Answer 2</label><br/>            
<input type="radio" name="q5" value="c" id="q5c"><label for="q5c">Answer 3</label><br/>           
<input type="radio" name="q5" value="d" id="q5d"><label for="q5d">Answer 4</label><br/>        
</ul>        

<p class="question">6. What is the answer to this question?</p>        

<ul class="answers">            
<input type="radio" name="q6" value="a" id="q6a"><label for="q6a">Answer 1</label><br/>            
<input type="radio" name="q6" value="b" id="q6b"><label for="q6b">Answer 2</label><br/>            
<input type="radio" name="q6" value="c" id="q6c"><label for="q6c">Answer 3</label><br/>            
<input type="radio" name="q6" value="d" id="q6d"><label for="q6d">Answer 4</label><br/>        
</ul>       

<p class="question">7. What is the answer to this question?</p>        

<ul class="answers">            
<input type="radio" name="q7" value="a" id="q7a"><label for="q7a">Answer 1</label><br/>            
<input type="radio" name="q7" value="b" id="q7b"><label for="q7b">Answer 2</label><br/>            
<input type="radio" name="q7" value="c" id="q7c"><label for="q7c">Answer 3</label><br/>            
<input type="radio" name="q7" value="d" id="q7d"><label for="q7d">Answer 4</label><br/>        
</ul>        

<p class="question">8. What is the answer to this question?</p>        

<ul class="answers">            
<input type="radio" name="q8" value="a" id="q8a"><label for="q8a">Answer 1</label><br/>            
<input type="radio" name="q8" value="b" id="q8b"><label for="q8b">Answer 2</label><br/>            
<input type="radio" name="q8" value="c" id="q8c"><label for="q8c">Answer 3</label><br/>            
<input type="radio" name="q8" value="d" id="q8d"><label for="q8d">Answer 4</label><br/>       
</ul>        

<p class="question">9. What is the answer to this question?</p>        

<ul class="answers">            
<input type="radio" name="q9" value="a" id="q9a"><label for="q9a">Answer 1</label><br/>            
<input type="radio" name="q9" value="b" id="q9b"><label for="q9b">Answer 2</label><br/>            
<input type="radio" name="q9" value="c" id="q9c"><label for="q9c">Answer 3</label><br/>            
<input type="radio" name="q9" value="d" id="q9d"><label for="q9d">Answer 4</label><br/>        
</ul>        

<p class="question">10. What is the answer to this question?</p>        

<ul class="answers">           
<input type="radio" name="q10" value="a" id="q10a"><label for="q10a">Answer 1</label><br/>            
<input type="radio" name="q10" value="b" id="q10b"><label for="q10b">Answer 2</label><br/>            
<input type="radio" name="q10" value="c" id="q10c"><label for="q10c">Answer 3</label><br/>            
<input type="radio" name="q10" value="d" id="q10d"><label for="q10d">Answer 4</label><br/>        
</ul>        

<br/>
<div id="results">            
Show me the answers!       
</div>                

<div id="category1">            
<p>              
<strong>Question 1:</strong> The correct answer is the <strong>Answer 1</strong>.</p>        
</div>        

<div id="category2">            
<p>              
<strong>Question 2:</strong> The correct answer is <strong>Answer 2</strong>.</p>        
</div>        

<div id="category3">            
<p>                
<strong>Question 3:</strong> The correct answer is <strong>Answer 3</strong>.</p>        
</div>        

<div id="category4">            
<p>               
<strong>Question 4:</strong> The correct answer is <strong>Answer 4</strong>.</p>        
</div>        

<div id="category5">            
<p>                
<strong>Question 5:</strong> The correct answer is <strong>Answer 1</strong>.</p>        
</div>        

<div id="category6">            
<p>                
<strong>Question 6:</strong> The correct answer is <strong>Answer 2</strong>.</p>        
</div>        

<div id="category7">            
<p>                
<strong>Question 7:</strong> The correct answer is <strong>Answer 3</strong>.</p>        
</div>        

<div id="category8">            
<p>               
<strong>Question 8:</strong> The correct answer is <strong>Answer 4</strong>.</p>        
</div>        

<div id="category9">           
<p>               
<strong>Question 9:</strong> The correct answer is <strong>Answer 1</strong>.</p>        
</div>        

<div id="category10">            
<p>                
<strong>Question 10:</strong> The correct answer is <strong>Answer 2</strong>.</p>        
</div>        

<div id="category11">            
<p>                
You answered them all right!</p>        
</div>
</body>
</html>

我的 iOS 编码遇到了一个大问题。我的要求是我有一个 HTML 文件。该文件包含一些多项选择问答。我将该 html 文件加载到 UIWebView 中。当我单击该回答按钮时,我必须检索在 webView 上单击了哪个按钮。

我必须从 html 文件中检索该方法。

这怎么可能?

它不是重复的 Q,我已经用谷歌搜索了这个,无论我得到什么,它都不满足我的要求。

我的代码如下:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *path;
    NSBundle *thisBundle = [NSBundle mainBundle];
    path = [thisBundle pathForResource:@"Untitled" ofType:@"html"];
    NSURL *instructionsURL = [NSURL fileURLWithPath:path];
   [_webview loadRequest:[NSURLRequest requestWithURL:instructionsURL]];
  }
  - (void)webViewDidFinishLoad:(UIWebView*)theWebView
  {
    NSString* returnValue =[theWebView stringByEvaluatingJavaScriptFromString:@"myFunction()"];
    NSLog(@"returnValue = %@ ",returnValue);
  }
  - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
  {
     NSLog(@" didFailLoadWithError");
  }

但我的输出是:Sample_JS[2218:44719] returnValue =

请帮帮我....

最佳答案

在您的代码中,这是事情发生的顺序:

  1. 您在 viewDidload 中加载请求
  2. 您的 webview 已完成加载。因此您获得了所选的值。但是此时。您还没有选择任何内容。
  3. 当你选择一个项目时,你没有做任何事情来告诉 iOS 你选择了一个

所以,我建议使用WKWebview,使用 JS Code 比较好。

  1. 在您的 js 代码中,添加一个提交按钮。 然后在按钮 Action 函数中

    function submit () {
        var message = //here get all your selected items
        window.webkit.messageHandlers.observe.postMessage(message);
     }
    
  2. 在ViewController中,创建WKUserContentController来处理通知

    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc]init];
    WKUserContentController *controller = [[WKUserContentController alloc]
                                       init];
    [controller addScriptMessageHandler:self name:@"observe"];
     configuration.userContentController = controller;
    _webView = [[WKWebView alloc] initWithFrame:self.view.frame
                              configuration:configuration];
    
  3. 委托(delegate)方法中,处理js事件

    -(void)userContentController:(WKUserContentController *)userContentController
      didReceiveScriptMessage:(WKScriptMessage *)message {
    //message.body
    }
    

更新整个项目

#import <WebKit/WebKit.h>
@interface ViewController ()<WKScriptMessageHandler>

@property (strong, nonatomic) WKWebView  * webview;

@end

@implementation ViewController

- (void)viewDidLoad {
    NSString *path;
    NSBundle *thisBundle = [NSBundle mainBundle];
    path = [thisBundle pathForResource:@"Untitled" ofType:@"html"];
    NSURL *instructionsURL = [NSURL fileURLWithPath:path];

    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc]init];
    WKUserContentController *controller = [[WKUserContentController alloc]
                                       init];
    [controller addScriptMessageHandler:self name:@"observe"];
    configuration.userContentController = controller;
    _webview = [[WKWebView alloc] initWithFrame:self.view.frame
                              configuration:configuration];
    [_webview loadRequest:[NSURLRequest requestWithURL:instructionsURL]];
    [self.view addSubview:self.webview];
}

-(void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
    NSLog(@"%@",message.body);
}

和html文件

<html>
<head>
<script type="text/javascript">
function getFirstSelect(){
    var rates = document.getElementsByName('q1');
    var rate_value;
    for(var i = 0; i < rates.length; i++){
        if(rates[i].checked){
            rate_value = rates[i].value;
            break;
        }
    }
    window.webkit.messageHandlers.observe.postMessage(rate_value);
}
</script>

<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<p class="question">1. What is the answer to this question?</p>

<ul class="answers">
<input type="radio" name="q1" value="a" id="q1a"><label for="q1a">Answer 1</label><br/>
<input type="radio" name="q1" value="b" id="q1b"><label for="q1b">Answer 2</label><br/>
<input type="radio" name="q1" value="c" id="q1c"><label for="q1c">Answer 3</label><br/>
<input type="radio" name="q1" value="d" id="q1d"><label for="q1d">Answer 4</label><br/>
</ul>

<div id="Submit">
     <button onclick="getFirstSelect()">Submit</button>
</div>

</body>
</html>

点击提交时

这是测试项目,相信你自己能搞定

关于javascript - 如何在 iOS 中调用 Javascript 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30588658/

有关javascript - 如何在 iOS 中调用 Javascript 方法?的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  3. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  4. ruby - 如何在 Ruby 中顺序创建 PI - 2

    出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits

  5. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  6. Ruby 方法() 方法 - 2

    我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby​​-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco

  7. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  8. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  9. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  10. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

随机推荐