SafariExtensionHandler.swift is not working at all

Hi all.


I'm following this guide for the creation of a new Safari App Extension.


However, once executed, nothing is logged inside the console when some events are fired.


For example, when I click to the toolbar icon, this following function


override func toolbarItemClicked(in window: SFSafariWindow) {
     // This method will be called when your toolbar item is clicked.
     NSLog("The extension's toolbar item was clicked")
}


is never executed, because in the console nothing is printed.


Also, the following function messageReceived does not log anything in the console


override func messageReceived(withName messageName: String, from page: SFSafariPage, userInfo: [String : Any]?) {
     // This method will be called when a content script provided by your extension calls safari.extension.dispatchMessage("message").
     page.getPropertiesWithCompletionHandler { properties in
          NSLog("The extension received a message (\(messageName)) from a script injected into (\(String(describing: properties?.url))) with userInfo (\(userInfo ?? [:]))")
     }
}


when the file script.js fires this event


document.addEventListener("DOMContentLoaded", function(event) {
    safari.extension.dispatchMessage("Hello World!");
});


In the info.plist file the class is declared like this:

$(PRODUCT_MODULE_NAME).SafariExtensionHandler


It seems like the entire SafariExtensionHandler.swift is never read.


The AppDelegate.swift is working normally, instead.


What's the problem here? The project contains brand new code generated by Xcode.


Thank you for you help.

Replies

What exactly console are you looking? NsLog messages will be printed out in console application, you can open it though spotlight search on your Mac. Just type console in spotlight search. Then you can filter messages by proccess name.