如何以编程方式启用 Safari App Extension? [英] How to enable Safari App Extension programmatically?

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

问题描述

我正在 macOS 应用中开发 Safari 应用扩展.当用户安装此应用程序时,扩展程序会添加到 Safari,但默认情况下是禁用的.我们可以使用 SFSafariExtensionManager 检测扩展状态类通过其 getStateOfSafariExtension 方法.

I'm developing a Safari App Extension inside a macOS app. When a user installs this app, the extension is added to Safari, but it's disabled by default. We can detect the state of extension by using SFSafariExtensionManager class via its getStateOfSafariExtension method.

现在我想以编程方式启用扩展状态,如果它被禁用.我怎样才能做到这一点?或者有人知道 macOS 中的首选项/应用扩展设置存储在哪里吗?

Now I want to enable the extension state programmatically if it is disabled. How can I achieve that? Or do anyone have any idea where the preferences / app extensions settings are stored in macOS?

推荐答案

您可以创建一个按钮,例如打开扩展首选项"以直接为您的扩展显示 Safari 首选项,然后用户可以启用它.

You can create a button such as "Open extension preferences" to show Safari preferences directly for your extension then the user could enable it.

您的应用代码:

import SafariServices

func enableExtension () {
    SFSafariApplication.showPreferencesForExtension(withIdentifier: YOUR_EXTENSION_IDENTIFIER) { (error) in
        NSLog("Error \(String(describing: error))")
    }
}

SFSafariApplication 只能在 Cocoa 应用中使用(不能在扩展中使用).

SFSafariApplication could be used in Cocoa app only (not extension).

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

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