ios - WKWebView cannot load local resource in HTML -
i have html pages load local app "documents" folder, structured directories. "main" folder store home page , resource, "interactive" folder store module, through homepage redirect.
i load "ipad.html" in "main" folder, page cannot load resources (i.e. css / js files) display correct style. code this.
let indexhtml = "ipad.html" let path = nssearchpathfordirectoriesindomains(filemanager.searchpathdirectory.documentdirectory, filemanager.searchpathdomainmask.userdomainmask, true) let documentdirectorypath: string = path[0] let folderpath = documentdirectorypath.appending("/main") let destinationurlforfile = url(fileurlwithpath: folderpath + "/\(indexhtml)") let baseurl = url(fileurlwithpath: folderpath, isdirectory: true) do{ let filename = try string(contentsof: destinationurlforfile, encoding: string.encoding(rawvalue: string.encoding.utf8.rawvalue)) webview.loadhtmlstring(filename, baseurl: baseurl) }catch{ print("loading html failed.") }
inside "ipad.html" this.
<!doctype html> <html> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge,chrome=1"> <title>demo</title> <meta name="description" content="" /> <meta name="author" content="" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> <link rel="stylesheet" href="./css/animate.min.css"> <link rel="stylesheet" href="./css/style.css"> <script src="./bower_components/modernizr/modernizr-2.5.3.min.js"></script> </head> <body class="page-ipad-landing animated" lang=""> ...... ...... </body> </html>
but move bundle.main, , remove "main" folder load "ipad.html" display correctly. why different behavior or ios not support complex folder structure under "documents" folder , how should correct it? thanks.
this simple 1 work in bundle.main folder.
if let url = bundle.main.url(forresource: "ipad", withextension: "html") { { let contents = try string(contentsoffile: url.path) webview.loadhtmlstring(contents, baseurl: url.deletinglastpathcomponent()) } catch { print("could not load html string.") } }
add html directories & files in project.
let webview = wkwebview() if let htmlpath = bundle.main.path(forresource: "directory/index", oftype: "html") { let htmlurl = url(fileurlwithpath: htmlpath, isdirectory: false) webview.loadfileurl(htmlurl, allowingreadaccessto: htmlurl) view = webview }
Comments
Post a Comment