ios 中 webview 加载的html 怎么和ios代码交互

如题所述

 IOS中webview与JS交互的代码:
  [webView stringByEvaluatingJavaScriptFromString:@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function myFunction() { "
"var field = document.getElementsByName('word')[0];"
"field.value='测试';"
"document.forms[0].submit();"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);"];
[webView stringByEvaluatingJavaScriptFromString:@"myFunction();"];
  上面的代码是把JS代码写入到html网页中,然后执行函数,实现JS中的功能。
  通过 stringByEvaluatingJavaScriptFromString 函数,可以实现webview强大的交互功能。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-04-15
给html传递数据用
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;
html调用原生,只能在
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
方法创建request加上你要传递给原生数据,其中加入一个Key,当方法运行时,截获这个Key,做你想要的处理,并返回NO;本回答被提问者和网友采纳