我下面Apple’s guide for creating a Safari App Extension。总之,我有:
扩展的工具栏按钮将出现在Safari。苹果的指南说我应该看到的NSLog消息在控制台当我点击工具栏按钮,但我没有看到任何东西。
我已经编辑SafariExtensionHandler.swift将消息发送到由扩展注入的脚本:
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")
window.getActiveTab(completionHandler: { (activeTab) in
activeTab?.getActivePage(completionHandler: { (activePage) in
activePage?.dispatchMessageToScript(withName: "toolbarItemClicked", userInfo: nil)
})
})
}
而且我已经编辑注入脚本(的script.js),以提醒信息:
safari.self.addEventListener("message", function (event) {
alert("We got a message from the extension! - " + event.name + ": " + event.message);
});
当我点击工具栏按钮(当我在webkit.org页面上,因为我已经留在默认设置SFSafariWebsiteAccess)警报出现,所以扩展工作并注册的点击。但我没有看到的NSLog在Xcode的控制台,或者控制台应用程序。
我是一个真正的Xcode的新手,所以我敢肯定,我失去了一些东西显而易见的 - 但为什么没有出现在控制台中的NSLog消息?
每当有人问我,而在第一次运行的Xcode虽然我没有输入管理员帐户的细节我注意到,在控制台应用程序,当我选择 - (我不以管理员身份运行,如果有差别。 SYSTEM.LOG,我刚刚看到一条消息,说“无法读取文件”,这might be related to not running as an administrator。)
切换到您的扩展计划(方案只是为了运行和停止按钮的右边),然后打运行。
弹出框将要求您选择一个应用程序来运行:选择Safari浏览器。
Safari浏览器的一个新实例应打开,你会开始看到在Xcode控制台日志输出。
如果你还没有签署您的应用,但是扩展,Safari会拒绝您的扩展,该控制台会显示这样的消息
2017-04-12 13:00:44.799843-0400 Safari[37188:2787364] [Extensions]
Computing the code signing dictionary failed for extension with
identifier com.your.app.extension
2017-04-12 13:00:44.799865-0400 Safari[37188:2787364] [Extensions]
Disabling and blocking extension with identifier:
com.your.app.extension
在这种情况下,你只需要重新勾选“允许未签名的扩展”在Safari的开发者菜单,并允许在首窗格中的扩展,在这之后,你应该是好去。
Xcode的10.2测试版附带了新的方法来实现这一目标:SFSafariToolbarItem.showPopover()
参考:https://developer.apple.com/documentation/safariservices/sfsafaritoolbaritem?changes=latest_minor