Reputation: 995
I am building an OS X app using Swift and one of the things I would like to do is to build an HTML string in code and then display that page in a web view.
I can load an NSURLRequest
into that webView, but I do not know how to display a "virtual" page so to speak.
If I load a preset page into my Xcode Project called main.html
, then I load it like this.
let localfilePath = NSBundle.mainBundle().URLForResource("main", withExtension: "html")
let myRequest = NSURLRequest(URL: localfilePath!)
self.webView.mainFrame.loadRequest(myRequest)
However, my html page body is currently within a string variable named body
.
Is there an easy way to do this with Swift? Do I need to create a temporary file on disk first, load it then delete it afterwards?
Can someone please suggest the best method for this?
Upvotes: 0
Views: 624
Reputation: 236360
You can use WKWebView method loadHTMLString to use a string as the contents of your webpage.
Upvotes: 2