iOS JS交互--UIWebView与JS的交互
2015-06-10 00:26
288 查看
原文链接: /article/2898804.html (转载有增改!)
http://www.iashes.com/2015-02-479.html
摘要 :两个最重要的方法:
JS示例代码:
方法实现:
1.
2.
以上即可简单实现JS交互,有特殊需求欢迎讨论。
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交互,有特殊需求欢迎讨论。
相关文章推荐
- android activity生命周期
- Android开源项目分类汇总
- 微信支付--统一支付接口被坑经历
- APP应用常见九格宫计算方法
- Android之SQLiteOpenHelper
- 拒绝ndk-build (Android Native Development Kit)
- Android 编程下设置 Activity 切换动画
- Android 高仿 QQ5.0 侧滑菜单效果 HorizontalScrollView
- Unity开发者总结的5点工作经验
- IOS8文件归档NSKeyedArchive的使用方法
- IOS8 addressbook获取全部联系人
- IOS 利用桥接整合通讯录读取全部联系人
- IOS8 通讯录修改添加联系人
- IOS Block随笔
- Android Activity活动1
- Android获取文件夹路径
- Android 属性动画(Property Animation) 完全解析 (上)
- Android 属性动画(Property Animation) 完全解析 (下)
- Android设备data目录下的文件在DDMS下的File Explorer读不到
- Android中定时执行任务的三种实现方法