前段时间做HTML5与原生之间的交互WKWebView
一点使用理解po出来分享下;
iOS8.0以后才能使用WKWebView;
创建过程:
@property (nonatomic,strong) WKWebView *wkWebView;
@property (nonatomic, strong) UIProgressView *progressView;//顶部加载进度条
- (WKWebView *)wkWebView {
if (!_wkWebView) {
_wkWebView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 64) configuration:[[WKWebViewConfiguration alloc] init]];
_wkWebView.navigationDelegate = self;
_wkWebView.UIDelegate = self;
[_wkWebView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
}
return _wkWebView;
}
- (UIProgressView *)progressView {
if (!_progressView) {
_progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
_progressView.progressTintColor = [UIColor blueColor];
_progressView.trackTintColor = [UIColor clearColor];
_progressView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 5);
}
return _progressView;
}
功能1:进度条实现加载进度
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void *)context {
__weak MYWebViewController *weakSelf = self;
if ([keyPath isEqualToString:@"estimatedProgress"] && object == self.wkWebView) {
[self.progressView setProgress:self.wkWebView.estimatedProgress animated:YES];
if (self.wkWebView.estimatedProgress >= 1.0) {
[UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaseInOut animations:^{
[weakSelf.progressView setAlpha:0.0];
} completion:^(BOOL finished) {
[weakSelf.progressView setProgress:0.0f animated:NO];
}];
}else {
self.progressView.alpha = 1.0;
}
}
}
功能2:利用JS调用原生方法;
功能3:通过JS给HTML5传递内容
注册方法『设置标题』
注册方法『通过JS传递内容』
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.wkWebView.configuration.userContentController addScriptMessageHandler:self name:@"setTitle"];
[self.wkWebView.configuration.userContentController addScriptMessageHandler:self name:@"getValue"];
}
与之对应的移除方法
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.wkWebView.configuration.userContentController removeScriptMessageHandlerForName:@"setTitle"];
[self.wkWebView.configuration.userContentController removeScriptMessageHandlerForName:@"getValue"];
[self.wkWebView.configuration.userContentController removeAllUserScripts];
}
实现相应代理方法
#pragma mark - WKScriptMessageHandler
/** 通过网页返回的方法名调用我们写的方法 */
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
if ([message.name isEqualToString:@"setTitle"]){//JS返回的字符串是『setTitle』则截取下来做相应操作,调用原生方法等;
self.title = message.body;
}
if ([message.name isEqualToString:@"getValue"]) {
[self.wkWebView evaluateJavaScript:[NSString stringWithFormat:@"getValue(%@)", @"需要传递的内容字符串"];
}
}
功能4:截取跳转链接
#pragma mark WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
__weak MYWebViewController *weakSelf = self;
NSString *successUrl = [navigationAction.request.URL absoluteString];
if ([successUrl containsString:@"www.baidu.com"]) {
decisionHandler(WKNavigationActionPolicyCancel);//禁止跳转,做相应操作
else {
decisionHandler(WKNavigationActionPolicyAllow);//允许跳转,做相应操作
}