- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用 WKWebView
从我的 Web View 中获取 javascript 消息。但是IOS控制台里面什么都没有出现...
我实现了这段代码:
- (void) webView:(WKWebView *)webView didFailLoadWithError:(NSError *)error {
self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20)];
self.webView.backgroundColor = [UIColor whiteColor];
self.webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.webView.scrollView.delegate = self;
[self.view addSubview:self.webView];
// Setup WKUserContentController instance for injecting user script
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc]
init];
WKUserContentController* userController = [[WKUserContentController alloc]init];
[userController addScriptMessageHandler: self name:@"notification"];
configuration.userContentController = userController;
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/test.php"]]];
}
- (void)userContentController:(WKUserContentController*)userContentController
didReceiveScriptMessage:(WKScriptMessage*)message {
NSLog(@"%@", message.body);
}
和 HTML/JS :
<div id="test" onclick="test();">test</div>
<script type="text/javascript" src="jquery.js"></script>
<script>
function test (){
alert('click');
$('#test').text('hello');
window.webkit.messageHandlers.notification.postMessage("click");
}
</script>
当我点击 div 时,我的页面没有收到任何日志(警报也不起作用,但测试 -> hello 有效)
感谢支持
最佳答案
第一个问题 - 您出于某种原因将 WKWebView
设置代码放入 webView:didFailLoadWithError:
方法中。因此,您应该仅在收到错误后才创建 WKWebView
。看起来很奇怪。我已将此代码移至 viewDidLoad
。
第二个问题 - 您需要将 WKWebViewConfiguration
实例传递给 -[WKWebView initWithFrame:configuration:]
。在你的代码中 configuration
完全没用。
第三个问题 - 您的 HTML 代码仅在删除下一行后才对我有用:
$('#test').text('hello');
我不是 HTML/JS 方面的专家。但我想您正在尝试使用 JQuery 而没有在您的 HTML 中引用它。
结果代码如下所示:
- (void)viewDidLoad
{
[super viewDidLoad];
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
WKUserContentController* userController = [[WKUserContentController alloc] init];
[userController addScriptMessageHandler:self name:@"notification"];
configuration.userContentController = userController;
self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
[self.view addSubview:self.webView];
self.webView.backgroundColor = [UIColor whiteColor];
self.webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[self.view addSubview:self.webView];
NSString *string = @"<div id=\"test\" onclick=\"test();\">test</div>\n" \
"<script type=\"text/javascript\" src=\"jquery.js\"></script>\n" \
"<script>\n" \
"function test (){\n" \
" alert('click');\n" \
" window.webkit.messageHandlers.notification.postMessage(\"click\");\n" \
"}\n" \
"</script>";
[self.webView loadHTMLString:string baseURL:nil];
}
- (void)userContentController:(WKUserContentController*)userContentController didReceiveScriptMessage:(WKScriptMessage*)message
{
NSLog(@"%@", message.body);
}
关于WKWebView 中的 javascript 桥不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35876715/
我正在阅读命令模式,并且看到来自不同站点的示例,这些示例似乎使用桥+命令模式来展示命令模式。 首先,来自维基百科:https://en.wikipedia.org/wiki/Command_patte
我有一个超低延迟程序,需要与 Windows COM 组件交互。最快的方法是什么?使用像 JACOB 这样的 COM 桥,或者编写 native COM 库并使用像 ZeroMQ 这样的消息传递总线发
我正在开发一个应用程序,该应用程序具有托管在 Glassfish 服务器上的 Java Web 界面和作为 Linux 守护进程实现的 C 内核。 我现在最大的问题是如何进行远程过程调用。我需要双向调
我已经在带有KVM的Fedora 17上设置了VM,并为KVM配置了桥接网络。主机和VM均使用手动IP配置,主机的IP为192.168.0.2,VM的为192.168.0.10。 从VM可以毫无问题地
主 GUI 基于 SWT。我正在通过单击按钮从 printPDF 类运行打印操作。 public void startPDFPrint() throws Exception { Display
我开始尝试使用 SWT-AWT 桥,但我无法为位于 Composite 内的 JPanel 找到合适的尺寸。谁能告诉我代码有什么问题吗? import java.awt.BorderLayout; i
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Troubleshooting ClassNotFoundException when using PHP/
我对 Linux 很陌生,但我正在尽力学习。我们有CentOS 6的专用托管服务器。它配置了Apache服务器作为我们的公共(public)网站。现在我们需要在上面安装 PHP JAVA Bridge
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 2 年前。 Improve this qu
有没有类似铁轨搭桥 Pylons 的脚手架?我一直在搜索谷歌,但只发现了这个叫做 dbsprockets 的东西,这很好,虽然可能对我的需要来说太多了。我真正需要的是基于 SQLAlchemy 模型的
我正在寻找可用于集成任何源代码控制管理系统的通用 git 桥(如 git-svn、git-p4、git-tfs)模板。 如果没有这样的模板,至少有一些关于如何在 git 端集成基本操作的说明(对于其他
我正在开发一个应用程序,我将在 UIWebView 中同时使用 HTML5 和 native iOS 框架。我知道我可以实现 JavaScript 和 Objective-C 之间的通信。是否有任何库
我目前观察到第 3 方库(即 restfb)正在使用 java.util.logging,我看到这些日志最终出现在 STDOUT 中,即使我没有在我的 logback 中配置 SLF4J 控制台附加程
我正在尝试让 aurelia-materialize 桥接插件工作,但到目前为止无法使用 jspm 完成,所以我尝试使用 webpack。我将 typescript-webpack 框架提取到一个文件
我想使用 java 将 SFTP 客户端连接到 FTP 服务器。我知道这两种技术彼此无关。我想要完成的是通过互联网连接到 FTP 服务器,而不使用两个端口或更改服务器配置。 Java 中有 SFTP-
我有 MS Access 数据库文件,文本字段中带有德语变音符号。当我尝试阅读它们时,我有字符串?字符而不是带有变音符号的字符。我尝试为数据库连接设置不同的字符集,例如 properties.setP
我感觉这是 wpf 中的一个错误。让我知道你们对此有何看法。 为了简单起见,我在 .net 4.0 中制作了演示示例 我有一个 ContentControl,其中 Content 绑定(bind)到
我正在尝试让 PHPJAVA 桥在我目前的 Apache2 设置上工作。 虽然,坦率地说,我不知道从哪里开始。 我已经下载了 JavaBridgeTemplate621.war,但不知道放在哪里。 感
我需要在 php 上运行一些 java 代码所以我找到了这个 http://php-java-bridge.sourceforge.net/pjb/index.php 所以我下载了tomcat,把Ja
我正在使用 OrientDb 并希望通过 PHP 运行 Gremlin 命令。有谁知道是否有任何桥梁或产品可以连接这两种语言? 虽然我知道 OrientDB-PHP 和 OrientDB-REST 包
我是一名优秀的程序员,十分优秀!