Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Load Local File or URL in NSWebView OSX, Not UIWebView

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!

like image 470
Jon Brown Avatar asked Aug 03 '14 00:08

Jon Brown


1 Answers

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.
}
like image 69
INSITE MOBILE Avatar answered Oct 10 '22 20:10

INSITE MOBILE