且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

iOS共享扩展程序:通过Safari中的上下文菜单共享时获取页面的URL

更新时间:2023-01-01 21:52:38

我花了一个下午的大部分时间来阅读这份文档,并尝试对扩展进行不同的排列,就像我一直想做的那样(我认为)您正在尝试做的事情.

I've spent an embarrassingly large part of an afternoon reading the docs on this and trying different permutations of extensions, as I was looking to do exactly (I think) what you were trying to do.

我得出的结论是,在iOS上无法实现此精确流程.如果用户选择文本并使用上下文菜单(即复制",查找",共享" ...),则扩展名唯一会收到的是带有所选文本的NSItemProvider,即而不是包含预处理javascript结果的列表.当他们从该菜单中选择共享"时,仅当您在扩展名的Info.plist文件中将NSExtensionActivationSupportsText设置为YES时,该扩展名才会显示.

I've concluded that this exact flow cannot be achieved on iOS. If the user selects text and uses the context menu (i.e. "Copy", "Look Up", "Share"...), the only thing your extension will ever receive is an NSItemProvider with the text that they selected, i.e. not a plist with the results of the preprocessing javascript. When they select Share from that menu, the extension shows up if and only if you've got NSExtensionActivationSupportsText set to YES in the extension's Info.plist file.

为了运行预处理javascript,根据

In order to run the preprocessing javascript, an extension has to have NSExtensionActivationSupportsWebPageWithMaxCount set to a value greater than 0, per the docs. If an extension gets called via the selected text context menu, that javascript file never runs.

但是,可以很接近达到所需的流量.如果用户使用的是Safari,并且选择了一些文本,然后点击Safari UI底部的共享"图标,而不是在上下文菜单中点击共享",则NSItemProvider作为plist返回,并且NSExtensionJavaScriptPreprocessingFile被运行.我的javascript文件如下所示:

However, it's possible to get pretty close to the desired flow. If the user is in Safari, and selects some text, and then, instead of tapping "Share" in the context menu, taps the Share icon at the bottom of the Safari UI, then the NSItemProvider comes back as a plist and the NSExtensionJavaScriptPreprocessingFile gets run. My javascript file looks like the following:

var Share = function() {};

Share.prototype = {
  run: function(arguments) {
    arguments.completionFunction({"URL": document.URL, "selectedText": document.getSelection().toString()});
  },
  finalize: function(arguments) {
    // alert shared!
  }
};

var ExtensionPreprocessingJS = new Share

这意味着返回到扩展名的plist对象同时具有页面的URL和selectedText.

which means that the plist object returned to the extension has both the URL of the page and the selectedText.

如果扩展的唯一目的是共享URL,而没有URL的纯文本不是明智的用例,则可能不应该将NSExtensionActivationSupportsText设置为YES.例如,像Pocket这样的应用程序已启用它,但是如果用户在Safari中选择了一些文本,然后尝试通过上下文菜单进行共享,则Pocket只能使用纯文本而没有页面URL不能做任何有意义的事情,因此它会弹出出现一条非常神秘的错误消息.

If the only purpose of an extension is the share URLs, and plain text without a URL isn't a sensical use case, you should probably not have NSExtensionActivationSupportsText set to YES. For example, an app like Pocket has it enabled, but if a user selects some text in Safari and then tries to share via the context menu, Pocket can't do anything meaningful with just the plaintext and no page URL, so it just pops up a pretty cryptic error message.

如果您想看一下,我也发布了我的扩展程序代码.

I've published the code for my extension as well if you want to have a look.