I have an HTML file that is imported in my Xcode project with an image inside. The image is also inside my project folder but when I try to load the HTML file in the UIWebView the image is never loaded.
This is my HTML file:
<!DOCTYPE html>
<html>
<body>
<img src="background.png"/>
</body>
</html>
And here is my Swift code to load the HTML file:
var htmlFile = NSBundle.mainBundle().pathForResource("Travel", ofType: "html")
var htmlString = try? String(contentsOfFile: htmlFile!, encoding: NSUTF8StringEncoding)
weeb.loadHTMLString(htmlString!, baseURL: nil)
What is going wrong here? Thank you.
I had the same issue. Here is my solution
create one folder for example html and put all html file along with your images and other resource. and then drag drop that html folder to your xcode project. xcode will ask you for copy at that time choose option "Create folder reference" and then finish, Check below screen shot.
So your folder structure look like this.
your html file will be
<!DOCTYPE html>
<html>
<body>
<img src="back.png"/>
</body>
</html>
To load this html file your webview use the below code.
let bundlePath = NSBundle.mainBundle().bundlePath
let htmlFile = "\(bundlePath)/html/sample.html"
let fileURL = NSURL(fileURLWithPath: htmlFile)
webview.loadRequest(NSURLRequest(URL: fileURL))