您的位置:首页 > 移动开发

iOS JS交互--UIWebView与JS的交互

2015-06-10 00:26 288 查看
原文链接: /article/2898804.html (转载有增改!)
http://www.iashes.com/2015-02-479.html
摘要 :两个最重要的方法:

//获取JS数据
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script;


//触发事件调用协议
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;


JS示例代码:

//1.网络数据情况下
<html>
<head>
<title>Home</title>
<script>
function jump()
{
var clicked=true;
window.location="/clicked";     //改变URL  注意:要使用"/"分隔符
alert("js alert :jump");   //此处处理js动作,可注释js不做操作  yuanjiee
}

</script>
</head>
<body>
<h1>Page One</h1>
<button onclick="jump()">Click me</button>    <!---触发jump()事件  yuanjilee--->
</body>
</html>

//2.本地数据情况下
<html>
    <head>
        <title>
            js test
        </title>
    </head>
    
    <script type="text/javascript">
        function returnMyWebSite(org,arrayParam){
            var url = "myapps:" + "&org=" + org;
            for(var i in arrayParam){
                url = url + "&" + i + "=" + arrayParam[i];
            }
            //网页操作
            <!--js在网页中显示信息-->
            document.write(url);
            <!--js跳转网页-->
            document.location = url;
        }
    
        </script>
    <body>
        
        <font size="100"> this is my website;</font><br>
        <input type="button" value="return dic" onclick="returnMyWebSite('iashes',{'name':'yuanjilee','age':22,'sex':'famale'})" style="height=90px;width=200px;" />
    </body>
</html>


方法实现:

1.

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(@"finished");
//请求完成之后可以获取当前网站的title,还有document等信息
NSString *pageTitle = [self->webView stringByEvaluatingJavaScriptFromString:@"document.title"];
NSLog(@"myTitle-------%@",pageTitle);
//可以将获取的标题设置为当前当行的title
self.navigationItem.title = pageTitle;

/*
document的一些属性:
location-位置子对象
document.location.hash // #号后的部分
document.location.host // 域名+端口号
document.location.hostname // 域名
document.location.href // 完整URL
document.location.pathname // 目录部分
document.location.port // 端口号
document.location.protocol // 网络协议(http:)
document.location.search // ?号后的部分
documeny.location.reload() //刷新网页
document.location.reload(URL) //打开新的网页
document.location.assign(URL) //打开新的网页
document.location.replace(URL) //打开新的网页
*/
}


2.

#pragma mark - webViewDelegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//总路径
NSString *requestString = [[[request URL]  absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding ];
NSLog(@"requestString2 = %@",requestString);
//绝对路径
NSLog(@"relativaPath2 = %@",request.mainDocumentURL.relativePath);

//获取URL并且做比较,判断是否触发了JS事件,注意有"/"
if ([request.mainDocumentURL.relativePath isEqualToString:@"/clicked"]) {

//本地处理
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"nativa" message:@"button clicked" delegate:nil cancelButtonTitle:@"sure" otherButtonTitles:nil, nil];
[alertView show];

return NO;
}
return  YES;
}
 


以上即可简单实现JS交互,有特殊需求欢迎讨论。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: