如何以编程方式打开Safari Extension ToolbarItem弹出窗口 [英] How to open Safari Extension ToolbarItem popover programmatically

查看:130
本文介绍了如何以编程方式打开Safari Extension ToolbarItem弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式触发Safari扩展工具栏项上的点击事件,以便在网页上发生某些情况后显示自定义弹出窗口。我正在使用新的Xcode扩展IDE,并且已经使用界面构建器构建了我的弹出窗口。当前,StackOverflow上的所有答案都处理在Safari扩展构建器而不是Xcode界面中构建的扩展。例如,我尝试注入Safari JS解决方案,例如:

I want to programmatically trigger a 'click' event on my Safari Extension toolbarItem so that my custom popover appears after something happens on the webpage. I'm using the new Xcode extension IDE and have built my popover using interface builder. All of the answers on StackOverflow currently deal with extensions built in the Safari extension builder and not in the Xcode interface. For example, I have tried injected Safari JS solutions like:

safari.extension.toolbarItems[0].showPopover();

但是什么也没有发生,而且我认为在Xcode中构建扩展程序时它不起作用。我不在乎是在注入的javascript中还是在我的SafariExtensionHandler.Swift文件中触发弹出窗口-我只需要一种无需用户单击工具栏项目即可显示它的方法。

But nothing happens, and I don't think it's supposed to work when you build extensions in Xcode. I don't care whether the popover is triggered in the injected javascript or in my SafariExtensionHandler.Swift file -- I just need a way to show it without a user click on the toolbar item.

我已使用扩展名的info.plist将弹出窗口与工具栏项目相关联,该链接已链接至以下页面:

I have associated my popover with my toolbar item using the extension's info.plist which I have linked to below:

https://i.stack.imgur.com/VxyLs.png

该扩展程序与本机CocoaOS应用程序捆绑在一起,并使用在WWDC '15上引入的新Xcode范例(以下链接)构建。我可以使用以下方法访问SafariExtensionHandler.Swift中的Toolbaritem:

The extension is bundled with a native CocoaOS app, and it's constructed using the new Xcode paradigm introduced at WWDC '15 (link below). I can access the Toolbaritem in SafariExtensionHandler.Swift with this method:

 func getToolbarItem(completionHandler: @escaping (SFSafariToolbarItem?) -> Void) {
    SFSafariApplication.getActiveWindow {$0?.getToolbarItem (completionHandler: completionHandler)}
}

但是toolbarItem对象似乎没有显示弹出窗口的方法。

But the toolbarItem object doesn't seem to have a method to show the popover.

https://developer.apple.com/videos/play/wwdc2016/214/

推荐答案

SFSafariApplication.getActiveWindow { (window) in
    window?.getToolbarItem(completionHandler: { (item) in
        item?.showPopover()
    })
}

这篇关于如何以编程方式打开Safari Extension ToolbarItem弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆