artworkad シ artworkad シ - 2 years ago 564
Swift Question

Load local html into UIWebView using swift

This one is driving me crazy early this morning. I want to load some local html into a web view:

class PrivacyController: UIViewController {

@IBOutlet weak var webView:UIWebView!

override func viewDidLoad() {
let url = NSURL(fileURLWithPath: "privacy.html")
let request = NSURLRequest(URL: url!)

The html file is located in the root folder of my project but is inside a group. The webview is blank for me. Any ideas whats wrong? I am on xcode 6.1 and running this example on my iphone 6.

Answer Source

To retrieve URLs for application resources, you should use URLForResource method of NSBundle class.


let url = NSBundle.mainBundle().URLForResource("privacy", withExtension:"html") 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download