How can I close a Safari App Extension popover programmatically?

亡梦爱人 提交于 2019-12-04 10:29:46

问题


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.

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.

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?.

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.


回答1:


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

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

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.

For example:

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

        SafariExtensionViewController.shared.dismissPopover()

        // do other stuff;
    }
}



回答2:


I did it by calling dismiss method like below

@IBAction func onLoginBtnClicked (_ sender: Any) {
    NSLog("Button clicked")
    self.dismiss(self)
}


来源:https://stackoverflow.com/questions/43989714/how-can-i-close-a-safari-app-extension-popover-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!