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)
}
}
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With