GURU
GURU

Reputation: 323

Can not load local html file in WKWebView Swift 4

I used this code before - everything worked

let localfilePath = Bundle.main.url(forResource: "nameFile", withExtension: "html")
let myRequest = NSURLRequest(url: localfilePath!)

WebView.loadRequest(myRequest as URLRequest)
self.view.addSubview(WebView)

Help please - now I use WKWebView.

How to open a local html file in the WKWebView? Thank you

Upvotes: 3

Views: 5964

Answers (2)

GURU
GURU

Reputation: 323

import WebKit

@IBOutlet weak var webView: WKWebView!


let htmlPath = Bundle.main.path(forResource: "NameFolder/FileName", ofType: "html")

 let url = URL(fileURLWithPath: htmlPath!)

 let request = URLRequest(url: url)
 webView.load(request)

Do not forget to put your folder with the files here Do not forget to put your folder with the files here

Upvotes: 7

Leo Dabus
Leo Dabus

Reputation: 236538

WKWebView has a method specific for loading local urls. No need to use a URLRequest, just pass your local file url. Btw naming a url object localfilePath is misleading. I suggest changing it to localfileURL.

Upvotes: 0

Related Questions