Your First Swift 4 & iOS 11 App - Part 42: | Ray Wenderlich

Learn how to add web views into your apps to display web pages.

This is a companion discussion topic for the original entry at

One interesting thing I noticed. When I drag and drop “BullsEye.html”, the html file was not read in “AboutViewController”. However if I Right Click and Select > “Add Files To Project” the html file has been detected. Just in case someone tries to trouble shoot this issue, this small trick is a time saver…

1 Like

It appears that WebView is deprecated. Should we be using WebKitView?

I had the same issue. Drage and drop definitely does not work.

Running into the same issue, I am now trying to use WKWebView instead but it seems like its breaking at this line,

if let url = Bundle.main.url(forResource: "BullsEye", withExtension: "html")

Hi, I found this solution
/** The issue is that the file isn’t being coping to your app bundle. To fix it:
click your project
Click your target
Select Build Phases
Expand Copy Bundle Resources
Click ‘+’ and select your file.

let url = Bundle.main.url(forResource: “BullsEye”, withExtension: “html”)
let htmlData = try? Data(contentsOf: url!)
let baseURL = URL(fileURLWithPath: Bundle.main.bundlePath)
webView.load(htmlData!, mimeType: “text/html”, characterEncodingName: “UTF-8”, baseURL: baseURL)

It did work for me. Thanks a lot!!!

Excellent. That worked for me as well. Thank you.

Hmm I have tried all of the above but my webView is still not loading the html file. Does anyone know what the problem could be? Thanks.

Actually, never mind. I had the webView code in the wrong place. I should have put it in viewDidLoad. oops

explicitly importing WebKit for WKWebView does the trick

This topic was automatically closed after 166 days. New replies are no longer allowed.

Continuing the discussion from Your First Swift 4 & iOS 11 App - Part 42: | Ray Wenderlich:

I’m having a fatal error on Thread 1: Unexpectedly found nil while unwrapping an Optional Value.

I reverted the error using WKWebView just like here:

My problem now is that the close and about autor buttons disappeared.

My AboutViewController.swift code:

import UIKit
import WebKit

class AboutViewController: UIViewController, WKUIDelegate {

@IBOutlet weak var webView: WKWebView!

@IBAction func close() {
    dismiss(animated: true, completion: nil)

override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView

override func viewDidLoad() {
    if let url = Bundle.main.url(forResource: "BullsEye", withExtension: "html") {
        if let htmlData = try? Data(contentsOf: url) {
            let baseURL = URL(fileURLWithPath: Bundle.main.bundlePath)
            webView.load(htmlData, mimeType: "text/html", characterEncodingName: "UTF-8", baseURL: baseURL)

override func didReceiveMemoryWarning() {


Hi everybody !

I did in a simpler way that worked, see:
First I created an Outlet referencing the WebKitView and then created the following code:

@IBOutlet weak var webView: WKWebView!

Inside viewDidLoad i put this:

let htmlPath = Bundle.main.path(forResource: “BullsEye”, ofType: “html”)
let url = URL(fileURLWithPath: htmlPath!)
let request = URLRequest(url: url)

That’s all… :wink:

Your code not working, I’ll just skip the webview part and continue with the course

I’ve done the same thing, just skipped. My app is working beside this feature.

1 Like

We have an updated version of the course coming soon that fixes this issue. In the meantime, skipping this and continuing on seems reasonable :]

1 Like

Yes, agreed. The app is working flawlessly except for that ”webHTML” bug.