当前位置: 代码网 > it编程>编程语言>Javascript > WKWebView使用遇到的坑--加载本地html及JS交互

WKWebView使用遇到的坑--加载本地html及JS交互

2024年08月06日 Javascript 我要评论
1. ios9以前版本读取本地HTML的问题当使用loadRequest来读取本地的HTML时,WKWebView是无法读取成功的,后台会出现如下的提示:Could not create a sandbox extension for /原因是WKWebView是不允许通过loadRequest的方法来加载本地根目录的HTML文件。而在iOS9的SDK中加入了以下方法来加载本地的HTML文...

1. ios9以前版本读取本地html的问题

解决方法如下

1.objective-c:

//将文件copy到tmp目录
- (nsurl *)fileurlforbuggywkwebview8:(nsurl *)fileurl {
    nserror *error = nil; if (!fileurl.fileurl || ![fileurl checkresourceisreachableandreturnerror:&error]) { return nil; } // create "/temp/www" directory nsfilemanager *filemanager= [nsfilemanager defaultmanager]; nsurl *temdirurl = [[nsurl fileurlwithpath:nstemporarydirectory()] urlbyappendingpathcomponent:@"www"]; [filemanager createdirectoryaturl:temdirurl withintermediatedirectories:yes attributes:nil error:&error]; nsurl *dsturl = [temdirurl urlbyappendingpathcomponent:fileurl.lastpathcomponent]; // now copy given file to the temp directory [filemanager removeitematurl:dsturl error:&error]; [filemanager copyitematurl:fileurl tourl:dsturl error:&error]; // files in "/temp/www" load flawlesly :) return dsturl; } //调用逻辑 nsstring *path = [[nsbundle mainbundle] pathforresource:@"indexoff" oftype:@"html"]; if(path){ if ([[uidevice currentdevice].systemversion floatvalue] >= 9.0) { // ios9. one year later things are ok. nsurl *fileurl = [nsurl fileurlwithpath:path]; [self.webview loadfileurl:fileurl allowingreadaccesstourl:fileurl]; } else { // ios8. things can be workaround-ed // brave people can do just this // fileurl = try! pathforbuggywkwebview8(fileurl) // webview.loadrequest(nsurlrequest(url: fileurl)) nsurl *fileurl = [self.filehelper fileurlforbuggywkwebview8:[nsurl fileurlwithpath:path]]; nsurlrequest *request = [nsurlrequest requestwithurl:fileurl]; [self.webview loadrequest:request]; } }

2.swift

//将文件copy到tmp目录
func fileurlforbuggywkwebview8(fileurl: nsurl) throws -> nsurl {
    // some safety checks var error:nserror? = nil; if (!fileurl.fileurl || !fileurl.checkresourceisreachableandreturnerror(&error)) { throw error ?? nserror( domain: "buggywkwebviewdomain", code: 1001, userinfo: [nslocalizeddescriptionkey: nslocalizedstring("url must be a file url.", comment:"")]) } // create "/temp/www" directory let fm = nsfilemanager.defaultmanager() let tmpdirurl = nsurl.fileurlwithpath(nstemporarydirectory()).urlbyappendingpathcomponent("www") try! fm.createdirectoryaturl(tmpdirurl, withintermediatedirectories: true, attributes: nil) // now copy given file to the temp directory let dsturl = tmpdirurl.urlbyappendingpathcomponent(fileurl.lastpathcomponent!) let _ = try? filemgr.removeitematurl(dsturl) try! fm.copyitematurl(fileurl, tourl: dsturl) // files in "/temp/www" load flawlesly :) return dsturl } //方法调用 var filepath = nsbundle.mainbundle().pathforresource("file", oftype: "pdf") if #available(ios 9.0, *) { // ios9. one year later things are ok. webview.loadfileurl(fileurl, allowingreadaccesstourl: fileurl) } else { // ios8. things can be workaround-ed // brave people can do just this // fileurl = try! pathforbuggywkwebview8(fileurl) // webview.loadrequest(nsurlrequest(url: fileurl)) do { fileurl = try fileurlforbuggywkwebview8(fileurl) webview.loadrequest(nsurlrequest(url: fileurl)) } catch let error as nserror { print("error: " + error.debugdescription) } }

2. wkwebview - wknavigationdelegate使用

例如:

- (void)webview:(wkwebview *)webview decidepolicyfornavigationaction:(wknavigationaction *)navigationaction decisionhandler:(void (^)(wknavigationactionpolicy))decisionhandler {

    nsurlrequest *request = navigationaction.request;
    wmpageactiontype actiontype = actiontypenone; wknavigationactionpolicy actionpolicy = wknavigationactionpolicyallow; if([request.url.absolutestring hasprefix:oc_close_request]){ actiontype = actiontypeclose; actionpolicy = wknavigationactionpolicycancel; } if(self.actiondelegate && [self.actiondelegate respondstoselector:@selector(webview:action:type:)]) { [self.actiondelegate webview:webview action:navigationaction type:actiontype]; } //这句是必须加上的,不然会异常 decisionhandler(actionpolicy); }

3.wkwebview-js执行方法

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com