如何以编程方式关闭Safari应用程序扩展弹出窗口? [英] How can I close a Safari App Extension popover programmatically?

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

问题描述

按照 Safari应用程序扩展编程指南.该扩展程序包含一个弹出窗口,当单击扩展程序的工具栏项.弹出窗口视图包含一些按钮,这些按钮链接到用户可以执行的操作.

I'm building a Safari App Extension using XCode 8.3 and Swift 3, following the Safari App Extension Programming Guide. The extension includes a popover that appears when the extension's toolbar item is clicked. The popover view contains a few buttons linked to actions the user can perform.

我要在执行操作后单击这些按钮之一以关闭弹出窗口.默认情况下,单击弹出框外部的任何位置都会将其关闭,但是无论是在指南中还是在

I want clicking one of these buttons to close the popover after its action has been performed. By default, clicking anywhere outside of a popover closes it, but I haven't been able to find any other way to close the popover, either in the guide or in the docs.

我知道 NSPopover 具有performClose方法,但是没有似乎是从扩展程序内部访问弹出窗口本身的一种方式:应用程序扩展程序仅允许您提供 SFSafariExtensionViewController ,其内容神奇地显示在弹出窗口中.

I know that NSPopover has a performClose method, but there doesn't appear to be a way to access the popover itself from within the extension: the app extension only lets you provide a SFSafariExtensionViewController, whose contents magically appear within the popover.

我也曾尝试使用dismissViewController,如此StackOverflow答案所述,但在我的视图控制器中始终为零,并且self.dismissViewController(self)只会崩溃并显示以下消息:

I've also tried using dismissViewController as described in this StackOverflow answer, but in my view controller self.presenting is always nil, and self.dismissViewController(self) just crashes the extension with the message:

dismissViewController:: Error: maybe this view controller was not presented?.

最后,我注意到了与编程有关的问题 打开工具栏项的弹出窗口在过去6个月中都没有得到答复.这使我怀疑苹果可能只是简单地限制了如何打开和关闭弹窗.即使是这种情况,也很高兴知道有哪些限制.

Lastly, I noticed a related question about programmatically opening the toolbar item popover has gone unanswered the past 6 months. This leads me to suspect Apple may simply have strict limits on how the popover can be opened and closed. Even if this is the case, it would be nice to know for sure what the limitations are.

推荐答案

我将添加一个答案,以防有人偶然发现此问题.

I'll add an answer in case anyone stumbles upon this question.

已将dissmissPopover()实例方法添加到SFSafariExtensionViewController类.这可以用来以编程方式关闭弹出窗口.

A dissmissPopover() instance method has been added to the SFSafariExtensionViewController class. This can be used to programatically close the popover.

在XCode中创建Safari应用程序扩展时提供的默认模板为您提供了一个SafariExtensionViewController类,该类扩展了SFSafariExtensionViewController并将共享实例作为名为"shared"的静态字段保存,因此您可以从该实例调用dismissPopover()方法.

The default template given when creating a Safari App Extension in XCode gives you a SafariExtensionViewController class that extends SFSafariExtensionViewController and holds a shared instance as a static field called 'shared', so you can call the dismissPopover() method from that instance.

例如:

class SafariExtensionHandler: SFSafariExtensionHandler {
    func myFunc() {
        // do stuff;

        SafariExtensionViewController.shared.dismissPopover()

        // do other stuff;
    }
}

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

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