一.实现iOS开发加载h5页面更好的选择是使用带有wkWebView的WebKit的框架,下面我将简单的就开发中与h5进行交互进行一个分享;
1.同h5沟通一个方法名进行交互开发,如@"function";
2.为此方法的注册一个block用来接收触发block,代码如下:
[self addMessageName:@"function" action:^(WKScriptMessage *message) {
NSString *uid = message.body;
if (uid.length > 0) {
ZLPersonalInfoModel *model = [[ZLPersonalInfoModel alloc] init];
model.uid = [uid integerValue];
ZLAnchorPageViewController *anchorVC = [[ZLAnchorPageViewController alloc] initWithModel:model];
ZLMainTabViewController *mainTabViewController = (ZLMainTabViewController *)kKeyWindow.rootViewController;
ZLNavigationController *nav = mainTabViewController.selectedViewController;
[nav pushViewController:anchorVC animated:YES];
}
}];
3.定义一个全局的字典类型;
4.在add的方法内为h5页面的代理设置为self,并将方法名传进去,接着,将block放入全局的字典用于在webview的代理方法中使用;
5.h5触发了方法后,会走- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message;方法,在方法中取出字典中的block,然后执行block;
6.wekview的代理方法总结
//开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation{
[self showLoading];
}
//当内容开始返回时调用
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation{
}
//页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
[self hideLoading];
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error;
{
[self hideLoading];
}
#pragma mark - WKScriptMessageHandler
// 支持 https
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler
{
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
}
}
#pragma mark - WKUIDelegate
//Alert弹框
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message?:@"" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:([UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
completionHandler();
}])];
[self presentViewController:alertController animated:YES completion:nil];
}
#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message
{
id action = self.messageDic[message.name];
if (action) {
messageActionBlock actionBlock = action;
actionBlock(message);
}
}
网友评论