gpt4 book ai didi

WKWebView 中的 javascript 桥不起作用

转载 作者:行者123 更新时间:2023-11-30 08:32:15 25 4
gpt4 key购买 nike

我尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com