OC端
- (void)loadWebPageWithString:(NSString*)urlString
{
NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[self setUserAgent];
[_webView loadRequest:request];
JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
context[@"test"] = ^() {
NSArray *args = [JSContext currentArguments];
for (JSValue *jsVal in args) {
NSLog(@"%@", jsVal);
}
}
web端
<html>
<head>
</head>
<body>
<button id="hallo" onclick="buttonClick()">按钮</button>
<script>
function buttonClick() {
test("参数1", "参数2"); //这里的参数个数无限制,可以空参数.(名称和OC端对应)。
}
}
</script>
</body>
</html>