WKWebView的使用(二)

之前讲述了WKWebView对于WKNavigationDelegate的使用,这里说一下剩下的东西。

一、 WKUIDelegate的使用

WKUIDelegate是WebKit对于用户交互的处理代理,它可以使用原生的提示框来代替JavaScript中的提示框,虽然JavaScript中可以做的和原生相似,但是如果有输入的处理的话毕竟还是不如原生的方便。在Delegate中提供了三种提示框的修改:Alert,Confirm,Prompt:

/*  警告 */
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
    [[[UIAlertView alloc] initWithTitle:@"警告框" message:message delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil] show];
    completionHandler();
}
///** 确认框 */
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler{
    [[[UIAlertView alloc] initWithTitle:@"确认框" message:message delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil] show];

    completionHandler(1);
}
/**  输入框 */
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler{
    [[[UIAlertView alloc] initWithTitle:@"输入框" message:prompt delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil] show];
 completionHandler(@"你是谁!");
}

​ 除此之外,Delegate中还有两个方法:

- (nullable WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures;用于在创建新的WebView时指定配置对象、导航动作对象、window特性。如果没用实现这个方法,不会加载链接,如果返回的是原webview会崩溃。

- (void)webViewDidClose:(WKWebView *)webView NS_AVAILABLE(10_11, 9_0);这个方法在关闭webView时进行调用,可以进行环境的清空等操作。

二、 本地资源的加载

WKWebView不支持直接加载bundle中的本地html,如果不进行处理的话,将会导致页面无法正常显示。如果只是需要加载一个单独的html文件,可以直接读取内容,然后使用- (nullable WKNavigation *)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;方法进行加载。但是如果是包含JS文件和css文件的话,就必须使用下面的方法了。

​ 在iOS9及以上版本的系统中,可以使用方法- (nullable WKNavigation *)loadFileURL:(NSURL *)URL allowingReadAccessToURL:(NSURL *)readAccessURL;进行加载,但是在iOS8系统中,必须把本地资源的内容进行copy,然后再进行加载。


- (NSString *)copyToDocumentPath:(NSString *)urlPath {
    NSString *wkWebViewPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"wkWebView"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:wkWebViewPath]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:wkWebViewPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString *dstPath = [wkWebViewPath stringByAppendingPathComponent:urlPath];

    if (![[NSFileManager defaultManager] fileExistsAtPath:dstPath]) {
        NSString *srcPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:urlPath];
        [[NSFileManager defaultManager] copyItemAtPath:srcPath toPath:dstPath error:nil];
    }

    return dstPath;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值