简体   繁体   English

如何以编程方式打开Safari Extension ToolbarItem popover

[英]How to open Safari Extension ToolbarItem popover programmatically

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. 我想以编程方式触发Safari Extension toolbarItem上的“click”事件,以便在网页上发生某些事情后出现我的自定义弹出窗口。 I'm using the new Xcode extension IDE and have built my popover using interface builder. 我正在使用新的Xcode扩展IDE,并使用界面构建器构建了我的popover。 All of the answers on StackOverflow currently deal with extensions built in the Safari extension builder and not in the Xcode interface. StackOverflow上的所有答案目前都处理Safari扩展构建器中内置的扩展,而不是Xcode接口中的扩展。 For example, I have tried injected Safari JS solutions like: 例如,我尝试过注入Safari JS解决方案,例如:

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

But nothing happens, and I don't think it's supposed to work when you build extensions in Xcode. 但没有任何反应,我不认为它在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. 我不关心弹出窗口是在注入的javascript中还是在我的SafariExtensionHandler.Swift文件中触发 - 我只需要一种方法来显示它而无需用户单击工具栏项。

I have associated my popover with my toolbar item using the extension's info.plist which I have linked to below: 我使用扩展名的info.plist将我的popover与我的工具栏项相关联,我已将其链接到下面:

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

The extension is bundled with a native CocoaOS app, and it's constructed using the new Xcode paradigm introduced at WWDC '15 (link below). 该扩展与本机CocoaOS应用程序捆绑在一起,并使用在WWDC '15(下面的链接)中引入的新Xcode范例构建。 I can access the Toolbaritem in SafariExtensionHandler.Swift with this method: 我可以使用以下方法访问SafariExtensionHandler.Swift中的Toolbaritem:

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

But the toolbarItem object doesn't seem to have a method to show the popover. 但toolbarItem对象似乎没有显示弹出窗口的方法。

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

Open the Info.plist of your Safari extension and locate the SFSafariToolbarItem key, then look for Action sub-key and make sure it's set to Popover . 打开Safari扩展的Info.plist并找到SFSafariToolbarItem键,然后查找Action子键并确保将其设置为Popover

Also, make sure that your SFSafariExtensionViewController subclass assigns the desired preferredContentSize . 此外,请确保您的SFSafariExtensionViewController子类指定所需的preferredContentSize

import SafariServices

class SafariExtensionViewController: SFSafariExtensionViewController {

    static let shared = SafariExtensionViewController()

    override func viewDidLoad() {
        super.viewDidLoad()

        preferredContentSize = NSSize(width: 330, height: 119)
    }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM