一.實(shí)現(xiàn)iOS開發(fā)加載h5頁面更好的選擇是使用帶有wkWebView的WebKit的框架拯辙,下面我將簡單的就開發(fā)中與h5進(jìn)行交互進(jìn)行一個(gè)分享挤安;
1.同h5溝通一個(gè)方法名進(jìn)行交互開發(fā)导梆,如@"function";
2.為此方法的注冊(cè)一個(gè)block用來接收觸發(fā)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.定義一個(gè)全局的字典類型丹擎;
4.在add的方法內(nèi)為h5頁面的代理設(shè)置為self砌滞,并將方法名傳進(jìn)去,接著念链,將block放入全局的字典用于在webview的代理方法中使用盼忌;
5.h5觸發(fā)了方法后,會(huì)走- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message掂墓;方法谦纱,在方法中取出字典中的block,然后執(zhí)行block;
6.wekview的代理方法總結(jié)
//開始加載時(shí)調(diào)用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation{
[self showLoading];
}
//當(dāng)內(nèi)容開始返回時(shí)調(diào)用
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(null_unspecified WKNavigation *)navigation{
}
//頁面加載完成之后調(diào)用
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
[self hideLoading];
}
// 頁面加載失敗時(shí)調(diào)用
- (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:@"確認(rèn)" 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);
}
}