#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>
@interface ViewController : UIViewController<WKNavigationDelegate,WKScriptMessageHandler,WKUIDelegate>
@end
.m
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) WKWebView *webView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
WKUserContentController *controller = [[WKUserContentController alloc] init];
[controller addScriptMessageHandler:self name:@"observe"];
configuration.userContentController = controller;
NSURL *jsUrl = [NSURL URLWithString:@"file:///Users/zhengzeqiang/Desktop/test.html"];
_webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
//打开左划回退功能
_webView.allowsBackForwardNavigationGestures =YES;
_webView.navigationDelegate = self;
_webView.UIDelegate = self;
[_webView loadRequest:[NSURLRequest requestWithURL:jsUrl]];
[self.view addSubview:_webView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message {
// Check to make sure the name is correct
if ([message.name isEqualToString:@"observe"]) {
// Log out the message received
NSLog(@"Received event %@", message.body);
NSString *sendCode = @"goToHtml();";
[_webView evaluateJavaScript:sendCode completionHandler:nil];
}
}
#pragma mark - WKNavigationDelegate
//根据webView、navigationAction相关信息决定这次跳转是否可以继续进行,这些信息包含HTTP发送请求,如头部包含User-Agent,Accept
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
decisionHandler(WKNavigationActionPolicyAllow);
NSLog(@"%@", navigationAction.request.URL);
switch (navigationAction.navigationType) {
case WKNavigationTypeBackForward: {
NSLog(@"WKNavigationTypeBackForward");
}
break;
case WKNavigationTypeFormResubmitted: {
NSLog(@"WKNavigationTypeFormResubmitted");
}
break;
case WKNavigationTypeFormSubmitted: {
NSLog(@"WKNavigationTypeFormSubmitted");
}
break;
case WKNavigationTypeLinkActivated: {
NSLog(@"WKNavigationTypeLinkActivated");
}
break;
case WKNavigationTypeOther: {
NSLog(@"WKNavigationTypeOther");
}
break;
case WKNavigationTypeReload: {
NSLog(@"WKNavigationTypeReload");
}
break;
default:
break;
}
}
@end
</pre><pre name="code" class="objc">.html
<pre name="code" class="html"><html>
<head>
<meta charset="UTF-8">
<style>
body{text-align:center}
.buttonClass {
height: 100px;
width: 500px;
font-size:50px;
}
</style>
</head>
<body>
<font size = >
<br>
<br>
<br>
<br>
<input type="button" value="发送命令" class="buttonClass" style="height=20px;width=100px;" οnclick="postMyMessage()"/>
<br>
<script language="javascript" type="text/javascript">
function postMyMessage()
{
var message = { 'message' : 'Hello, World!', 'numbers' : [ 1, 2, 3 ] };
window.webkit.messageHandlers.observe.postMessage(message);
}
function goToHtml(){
<span style="white-space:pre"> </span>//do something!
}
</script>
</body>
</html>