2

I'm having a hard time with this one. I'm not really sure what I'm doing wrong.

Here's how I'm doing it for iOS UIWebView:

    let url = Bundle.main.url(forResource: "index", withExtension: "html")
    let request = NSURLRequest(url: url!)
    webView.loadRequest(request as URLRequest)

Unfortunately, that doesn't work for an OS X app. I found some code which used to work with Swift 2. I made some changes to it to make it work with Swift 3, but I'm still getting an error.

    let url = Bundle.main.url(forResource: "index", withExtension: "html")
    self.webView.mainFrame.load(NSURLRequest(URL: NSURL(string: url)!))

What are my options? Thanks in advance!

2 Answers2

5

Try this simple steps, for me works

let urlpath = Bundle.main.path(forResource: "index", ofType: "html");
let requesturl = URL(string: urlpath!)
let request = URLRequest(url: requesturl!)
webView.mainFrame.load(request)
Scinfu
  • 1,081
  • 13
  • 18
3

I have fixed my issue using sdbrain's answer on another thread.

Basically remove your root folder from your project and opt it to another folder. Then, drag the root folder from finder to Xcode directly. You will see options of creating groups for added folders or creating folder references. And a checkbox displaying copy if needed. I have selected, copy if needed options and creating folder references options.

My below code worked

    if let path = Bundle.main.path(forResource: "index", ofType: "html", inDirectory: "wwwroot") {
        webView.loadRequest(URLRequest(url: URL(fileURLWithPath: path)) )
    }
Community
  • 1
  • 1
Gultekin
  • 170
  • 1
  • 12