Dan DeLuca
Dan DeLuca

Reputation: 117

Loading HTML into Webview from a string, OS X

I would like to create some formatted text using HTML in my OS X program. I am programing in swift. I created a web view and set up a simple test.

import Cocoa
import RealmSwift
import WebKit

class invociegenerator: NSViewController {
@IBOutlet var InvoiceView: WebView!

override func viewDidLoad() {
    super.viewDidLoad()
    var htmlString:String! = "<br /><h2>Hello World!!</h2>"
    InvoiceView.loadHTMLString(htmlString, baseURL: nil)
        // Do view setup here.
}

}

I receive an error saying "WebView does not have a member named 'LoadHTMLString'" I must be missing something, is it possible to load from a string in an OS X program? There are plenty of tutorials online but the all focus on iOS.

Upvotes: 1

Views: 2186

Answers (1)

Cory
Cory

Reputation: 2312

"loadHTMLString" is a method on a WebFrame object not WebView. However, Webview does have a method that returns the mainFrame for the webview. So you could just call mainFrame on the webview like this.

InvoiceView.mainFrame.loadHTMLString(htmlString, baseURL: nil)

Upvotes: 5

Related Questions