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!) webView.loadRequest(request) } }
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.
How to load a HTML string into a WKWebView or UIWebView: loadHTMLString() If you want to load resources from a particular place, such as JavaScript and CSS files, you can set the baseURL parameter to any URL .
Use the loadHTMLString(_:baseURL:) method to begin loading local HTML files or the loadRequest(_:) method to begin loading web content. Use the stopLoading() method to stop loading, and the isLoading property to find out if a web view is in the process of loading.
To retrieve URLs for application resources, you should use URLForResource
method of NSBundle
class.
Swift 2
let url = NSBundle.mainBundle().URLForResource("privacy", withExtension:"html")
Swift 3
let url = Bundle.main.url(forResource: "privacy", withExtension: "html")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With