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执行方法
发表评论