Ben Ben - 10 months ago 68
iOS Question

iOS - Could not load NIB in bundle

I am writing an iOS app in Swift which needs to display a various static HTML files in a


My plan is to create a View Controller with an
property for the name of the file to display. The
will load the file specified and then create a
to display it.

When I run the app in the simulator (or on my device) and load the view, I get the following error message in the Xcode console:

Failed to set (filePath) user defined inspected property on (MyApp.HTMLViewController): Could not load NIB in bundle: 'NSBundle </Users/me/Library/Developer/CoreSimulator/Devices/A5982A49-71CD-4F06-ABD7-01EBBF001698/data/Containers/Bundle/Application/032D479A-091B-4FAB-9CB8-82DC72B99E89/> (loaded)' with name 'pyN-vU-Kur-view-0DN-tc-q8g'

The app does load and run. The HTMLViewController is set up to be a "show" segue from an accessory on a UITableView. Only when I tap the accessory does this error appear. The app still shows the (empty) view, I am able to use the navigation controller to go back to the UITableView just fine.

Using some
, I found that
is the Object ID for the View Controller, and
is the ID of the UIView inside it.

I checked my Build Phases, and noted that the storyboard is listed in the Copy Bundle Resources section. I thought perhaps it was just an issue where a reference got broken in my storyboard, so I deleted the scene and recreated it, but am still seeing the same error. I also saw some posts that talk about checking the Target Membership in the inspector. I have verified that the
folder which contains the HTML file, the storyboard, and the view controller all have the target membership box checked.

Debugging reveals that the HTML file is being loaded (I had the content logged to the console) and the
method is being called, and the error seems to occur on the call to

code is below:

import UIKit
import WebKit

class HTMLViewController: UIViewController {
var filePath: String! = "" {
didSet {
if let path = Bundle.main.path(forResource: filePath, ofType: "html", inDirectory: "content") {
do {
let contents = try String(contentsOfFile: path)
self.applyContent(html: contents)
} catch {
print("problem reading file \(filePath)")
} else {
print("problem locating file \(filePath)")

func applyContent(html: String) {
let webView = WKWebView(frame: self.view.bounds)
webView.loadHTMLString(html, baseURL: nil)

What is causing this error, and how can I fix it? I am relatively new to Swift and iOS development, so I may be missing something basic.

ray ray
Answer Source

I think the problem may be that you're trying to add the WKWebView to the view when it hasn't been loaded yet.

IBInspectables are set very early in the process of loading the view. Try adding your WKWebView in viewDidLoad or viewWillAppear.

override func viewWillAppear(animated: Bool) {
    let contents = try String(contentsOfFile: filePath)
    let webView = WKWebView(frame: self.view.bounds)
    webView.loadHTMLString(contents, baseURL: nil)