iOS WKWebview loadHTMLString(_ baseURL:) fails to load images and read css
I wish I had a better answer and if anybody has a better answer I will gladly accept it.
I ended up moving my files to the temp directory :( I've used this -
extension FileManager
{
func copyToTemporaryDirectory(itemIn itemURL: URL) -> URL?
{
let tempDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory())
let fileName = itemURL.lastPathComponent
let copyURL = tempDirectoryURL.appendingPathComponent(fileName)
let fileManager = FileManager.default
do {
if fileManager.fileExists(atPath: copyURL.path)
{
try fileManager.removeItem(at: copyURL)
}
try FileManager.default.copyItem(at: itemURL, to: copyURL)
return copyURL
}
catch let error
{
logger.debug("copyItem error: \(error.localizedDescription)")
return nil
}
}
}
and then loadHtmlString
with the URL
to this directory. from this directory, WKWebview is allowed to get everything.
WkWebView won't loadHTMLstring
SWIFT
You might be making it a bit too complicated. This worked for me ...
First, import WebKit
Second, create an @IBOutlet under your class:
@IBOutlet weak var webView: WKWebView!
Third, place the following in your viewDidLoad():
let htmlString:String! = "\(YourString)"
webView.loadHTMLString(htmlString, baseURL: NSBundle.mainBundle().bundleURL)
SWIFT 3 update
webView.loadHTMLString(htmlString, baseURL: Bundle.main.bundleURL)
WKWebView cannot load local resource in HTML
Add all the HTML directories & files in your 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
}
Related Topics
Mobile Safari Position:Fixed Z-Index Glitch When Scrolling
Unexpected Behavior When Casting an Nsnumber to Float
If No Table View Results, Display "No Results" on Screen
How to Open Maps App Programmatically with Coordinates in Swift
Uibutton Doesn't Listen to Content Mode Setting
Default Tab Bar Item Colors Using Swift Xcode 6
How to Limit the Number of Decimal Points in a Uitextfield
Ios5 Images Disappear When Scrolling with Webkit-Overflow-Scrolling: Touch
Convert String With Unknown Format (Any Format) to Date
Webkit Overflow Scrolling Touch CSS Bug on iPad
Which Is the Best Way to Estimate Measure of Photographed Things
Ask for User Permission to Receive Uilocalnotifications in iOS 8
How to Remove the Xcode Warning Apple MACh-O Linker Warning 'Pointer Not Aligned at Address
Facebook Sdk: App Not Registered as a Url Scheme
Avaudioplayer Throws Breakpoint in Debug Mode
"Your Binary Is Not Optimized for iPhone 5" (Itms-90096) When Submitting
How to Add Uipickerview into Uialertcontroller (Alert or Actionsheet) in Swift