swiftwkwebviewsafari-app-extension

Safari App Extension: WKWebKit in Popover not loading content


The objective To implement a Safari App Extension that shows a popover with a WebView inside that shows a webpage.

The problem The popover shows up correctly in Safari, clicking on it brings up a blank popover with nothing in it.

Based on logs in Console, the viewDidLoad() message shows up correctly but the popover is just blank. What did I do wrong here?

Thank you!

Here's my codes: SafariExtensionViewController.swift

import SafariServices
import WebKit
import os.log

class SafariExtensionViewController: SFSafariExtensionViewController, WKNavigationDelegate {

    static let shared = SafariExtensionViewController()

    var webView : WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "safari")    
        self.preferredContentSize = NSMakeSize(300, 450)
        webView = WKWebView(frame: self.view.frame)
        self.view.addSubview(webView)
        webView.navigationDelegate = self
        webView.translatesAutoresizingMaskIntoConstraints = false;

        let height = NSLayoutConstraint(item: webView, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 1, constant: 0)
        let width = NSLayoutConstraint(item: webView, attribute: .width, relatedBy: .equal, toItem: self.view, attribute: .width, multiplier: 1, constant: 0)
        self.view.addConstraints([height,width])

        let startingUrl = URL(string: "https://www.google.com")
        webView.load(URLRequest(url: startingUrl!))

        os_log("viewDidLoad() has finished.", log: log)
    }

}

Solution

  • Found the problem! It turns out, for extensions to access the network, you need to go to the target -> Capabilities section, turn on App Sandboxing and enable Outgoing Network Requests.