Search code examples
swiftmacosurlnsviewcontroller

Is there a way to contain a url within a controller in macOS swift 5


Thanks to this q/a, I've added a URL to an NSTextField. Is it possible to contain the web page in my own view controller?


Solution

  • You can import the WebKit framework and use a WKWebView instance for showing web pages.

    Documentation for WKWebView: https://developer.apple.com/documentation/webkit/wkwebview

    WKWebView allows to load a url as a usual web browser and it also can read HTML/CSS code that stored as a string. So much powerful tool.

    My open source project that highlights swift code for web sites uses WKWebView for previewing generated HTML/CSS output, here is the ViewController:

    https://github.com/uberdeviant/Swiftlighter/blob/master/Swiftlighter/Contollers/WebPreviewViewController.swift