问题
In loadHTMLStringImage function i have been loaded index.html
let htmlPath = Bundle.main.path(forResource: "index", ofType: "html")
let htmlUrl = URL(fileURLWithPath: htmlPath!, isDirectory: false)
self.webView.loadFileURL(htmlUrl, allowingReadAccessTo: htmlUrl)
After loading I have been checking md5 string. From the md5 zip file, I am unzipping to the local directory and got the local file directory URL. And passing the local directory URL to iframe src. But it shows an empty screen.
In console it shows:
Received an unexpected URL from the web process: 'file:///var/mobile/Containers/Data/Application/CECB33DC-8DE8-4FB0-BD28-73D46B47CB7C/Documents/appday/gng/v0-8/build/index.html'
2020-02-11 16:55:53.659227+0530 appday[15903:3679464] [Process] 0x10791c018 - WebPageProxy::Ignoring request to load this main resource because it is outside the sandbox
In viewDidLoad I have WKPreference too:
webView.navigationDelegate = self
webView.uiDelegate = self
webView.allowsBackForwardNavigationGestures = true
webView.configuration.preferences.javaScriptEnabled = true
webView.configuration.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")
Here, I am loading the local directory URL to iframe src. And df is the unzipped file local directory URL path:
My URL path looks like this:
'file:///var/mobile/Containers/Data/Application/CECB33DC-8DE8-4FB0-BD28-73D46B47CB7C/Documents/appday/gng/v0-8/build/index.html'
let url_path = df.absoluteString
let setPath = url_path
let js = "setFrame('" + setPath)
print("js::\(js)")
Here I am trying to load local file directory URL to iframe src.
Here is my local index.html file javascript where i am loading iframe src:
<iframe id='formFrame' src="" width="100%" height="100%" frameborder="0" scrolling="yes" allowfullscreen webkitallowfullscreen mozallowfullscreen oallowfullscreen msallowfullscreen></iframe>
function setFrame(url) {
document.getElementById('formFrame').src = url;
}
[Note: I am iframe content from a local directory URL not loading in local HTML file]
Any help much appreciated pls...
回答1:
import UIKit
import WebKit
class EventWallVC: UIViewController {
let directoryName : String = "appday/gng/v0-8/build"
//MARK: - IBOutlet
@IBOutlet weak var webView: WKWebView!
@IBOutlet weak var lblEventWall: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
SVProgressHUD.show()
self.webView.navigationDelegate = self
var indexfileurl = URL(string: "")
let path = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("\(directoryName)/\(index.html)")
indexfileurl = URL(fileURLWithPath: path)
//let url = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "")! /* your local bundle in save index.html file to get this */
self.webView.loadFileURL(indexfileurl, allowingReadAccessTo: indexfileurl)
let request = URLRequest(url: indexfileurl)
self.webView.load(request)
}
}
extension EventWallVC: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
SVProgressHUD.dismiss()
}
}
来源:https://stackoverflow.com/questions/60168084/how-to-load-local-directory-inside-iframe-in-ios-swift