Ok, so been working on this issue for a while now, trying to load a local HTML file or URL to a web view in Swift for OS X not iOS.
@IBOutlet weak var Webview: WebView!
func applicationDidFinishLaunching(aNotification: NSNotification?) {
// Insert code here to initialize your application
var url = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("pageName", ofType: "html"))
//var url = NSURL(string: "http://www.google.com")
var request = NSURLRequest(URL: url)
Webview.loadRequest(request)
}
So I know the above works with iOS and UIKit but it does not work in OS X it gives the error Webview does not have a member named loadRequest and it does not seem to have any methods for loading a URL any help is appreciated!
You must revise your code this way to request the URL as a string first. This works perfectly in swift without caching assets etc:
@IBOutlet var webView: UIWebView!
var urlpath = NSBundle.mainBundle().pathForResource("index", ofType: "html");
func loadAddressURL(){
let requesturl = NSURL(string: urlpath!)
let request = NSURLRequest(URL: requesturl)
webView.loadRequest(request)
}
override func viewDidLoad() {
super.viewDidLoad()
loadAddressURL() //func above
// Do any additional setup after loading the view, typically from a nib.
}
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