I have build a very simple WKWebView app using XCode 11 and Swift 5.
To do this I started with the default Hello World App and removed all the code from the two core AppDelegate methods and deleted the ContentView.swift file.
I then added a new class file ViewController.swift with the following code
import Cocoa
import WebKit
class ViewController: NSViewController, WKUIDelegate
{
var webView: WKWebView!
override func loadView()
{
let webConfiguration = WKWebViewConfiguration ()
webView = WKWebView (frame: CGRect(x:0, y:0, width:800, height:600), configuration:webConfiguration);
webView.uiDelegate = self ;
view = webView;
}
override func viewDidLoad()
{
super.viewDidLoad()
if let url = Bundle.main.url ( forResource: "TeamMap"
, withExtension: "html"
, subdirectory: "TM-MAC")
{
self.webView.loadFileURL ( url
, allowingReadAccessTo: url);
self.view = webView ;
}
}
}
I then added a View Controller to the storyboard and linked it to the above swift file and set it as the primary view controller.
I then added a set of files to a new folder TM-MAC in the add folder structure with the following files
TeamMap.html
TeamMap.js
TeamMap.css
plus several other javascript and image files.
When this app is run a window appears within which the WKWebView can be seen and also inspected using Safari.
I find that the html and js file have both loaded but that the css file has not. I am convinced there is no error in the css file as I have used the same three files in another XCode app written in Objective C.
I have found various similar reports inside StackOverflow and one thing I tried that still does not work is to change the variable passed into loadFileURL/allowingReadAccessTo from url to url.deletingLastPathComponent()
The answer to this is add the following line of code to override func loadView() before creating the web view.
webConfiguration.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs");