6

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)
    }

}
msharp
  • 81
  • 5

2 Answers2

5

If you are working with safari extension app, you need to add permissions for network requests in .entitlements file of extension.

<key>com.apple.security.network.server</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>

Add these lines, It worked for me.

Vikas Keskar
  • 1,158
  • 9
  • 17
  • 1
    Thanks! It took me hours to get this... What is weird is that I had the checkboxes enabled in xcode capabilities tab (as I found similar answer somewhere else). BUT IT WAS NOT SAVED TO .entitlements file for some reason... – Michal Roharik Nov 05 '18 at 17:15
2

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.

msharp
  • 81
  • 5