首页 > 代码库 > Xcode6 UIWebView与JavaScript交互(issue fix)

Xcode6 UIWebView与JavaScript交互(issue fix)

这篇文章中,有介绍UIWebView与JavaScript交互,在UIWebView截获JavaScript请求处理,从app的角度,这是JavaScript的Hook请求。 

在Xcode6之前的App版本,JavaScript可以这样将Hook请求发送出来:

HTML:

<li><a href=http://www.mamicode.com/"testapp://function=test&callbackJS=testCallback">test link
JavaScript:

<script language="JavaScript" type="text/javascript">	
  function xcode6(){
		var url = apptest://function=test&callbackJS=testCallback;
		location.href = url;
	  }

</script>
<li><a href="Javascript:xcode6();">Xcode6 Javascript</a></li>


在Xcode6的App中,发现通过JavaScript的改变href的方法,不可用了。从app的角度, (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType,并不会被调用。


通过escape URL, 来解决该问题:

<script language="JavaScript" type="text/javascript">	
  function xcode6(){
		var url = 'apptest://'+escape('function=test&callbackJS=testCallback');
		location.href = url;
	  }

</script>
<li><a href="Javascript:xcode6();">Xcode6 Javascript</a></li>





Xcode6 UIWebView与JavaScript交互(issue fix)