0

I am able to load up an external URL in a UIWebView like so:

let url = NSURL(string: "http://www.google.com")
let request = NSURLRequest(URL: url)
webView.loadRequest(request)

but how do I load a local html file?

The answer in this solution looked promising but Xcode says NSString.stringWithContentsOfFile() is deprecated.

1
  • It is. Show how you've tried to implement it so we can help fill in the gaps. Commented Sep 15, 2014 at 1:36

2 Answers 2

4

You can do essentially what you're doing now, just use NSBundle to get a URL for your local file, like this:

if let url = NSBundle.mainBundle().URLForResource("index", withExtension: "html") {
    webView.loadRequest(NSURLRequest(URL: url))
}
Sign up to request clarification or add additional context in comments.

Comments

1

Thanks @Nate Cook. Here is the update to Swift 3

    if let url = Bundle.main.url(forResource: "about", withExtension: "html") {
        webView.loadRequest(NSURLRequest(url: url) as URLRequest)
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.