Ben Ben - 1 month ago 18
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

WKWebView
.

My plan is to create a View Controller with an
@IBInspectable
property for the name of the file to display. The
HTMLViewController
will load the file specified and then create a
WKWebView
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/MyApp.app> (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
grep
, I found that
pyN-vU-Kur
is the Object ID for the View Controller, and
0DN-tc-q8g
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
content
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
applyContent
method is being called, and the error seems to occur on the call to
WKWebView()
.

My
HTMLViewController.swift
code is below:

import UIKit
import WebKit

@IBDesignable
class HTMLViewController: UIViewController {
@IBInspectable
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)
self.view.addSubview(webView)
}
}


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

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)
    self.view.addSubview(webView)
}
Comments