WKWebView. Load HTML File from App Bundle.

In Swift, you can load HTML files into your WKWebView from a file that is part of your App Bundle. Before you use the below code snippet, please make sure that the HTML file you want to display in WKWebView is added to your project.

Load Local HTML File to a WKWebView

let myUrl = myProjectBundle.url(forResource: "my-html-file", withExtension: "html")!
myWebView.loadFileURL(myUrl,allowingReadAccessTo: myUrl)

where:

my-html-file – is the name of HTML file you want to load into WKWebView from your App Bundle. Please note there is no need to provide file extension here.
myWebView – is an instance of WKWebView

For more Swift code examples and tutorials, please check the Swift Code Examples page on this website.


Leave a Reply

Your email address will not be published. Required fields are marked *

Free Video Lessons

Enter your email and stay on top of things,

Subscribe!