Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

}
like image 266
msharp Avatar asked Jul 24 '18 15:07

msharp


People also ask

Why are my extensions not working in Safari?

Enable the Safari web extension in iOS. In Safari, tap the More menu, then tap Extensions to select and enable your extension. Alternatively, open the Settings app, then choose Safari > Extensions. Find your extension in the list, tap it, then tap the switch to enable it, if necessary.


2 Answers

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.

like image 68
Vikas Keskar Avatar answered Nov 15 '22 08:11

Vikas Keskar


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.

like image 40
msharp Avatar answered Nov 15 '22 07:11

msharp