iOS共享扩展程序:通过Safari中的上下文菜单共享时获取页面的URL [英] iOS Share Extension: get URL of page when sharing via context menu in Safari

查看:215
本文介绍了iOS共享扩展程序:通过Safari中的上下文菜单共享时获取页面的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的

我正在尝试实现以下用户流:

I'm trying to achieve the following user flow:

  1. 用户正在iOS Safari中浏览网页.
  2. 用户选择一些内容(文本和图像),然后等待上下文菜单出现.
  3. 用户选择共享..."项.
  4. 用户在底部显示的共享菜单中选择我的App Extension.
  5. 所选内容和网页URL通过HTT呼叫与远程服务器共享.

我尝试过的事情

我通过Xcode进行了共享扩展.这是我的info.plistNSExtension部分:

I made a Share extension via Xcode. Here's the NSExtension section of my info.plist:

<key>NSExtension</key>
<dict>
    <key>NSExtensionAttributes</key>
    <dict>
        <key>NSExtensionActivationRule</key>
        <dict>
            <key>NSExtensionActivationSupportsWebPageWithMaxCount</key>
            <integer>1</integer>
            <key>NSExtensionActivationSupportsText</key>
            <true/>
            <key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
            <integer>1</integer>
        </dict>
        <key>NSExtensionJavaScriptPreprocessingFile</key>
        <string>test</string>
    </dict>
    <key>NSExtensionMainStoryboard</key>
    <string>MainInterface</string>
    <key>NSExtensionPointIdentifier</key>
    <string>com.apple.share-services</string>
</dict>

这是test.js文件:

var GetURL = function() {};
GetURL.prototype = {
run: function(arguments) {
    arguments.completionFunction({"URL": document.URL});
}
};
var ExtensionPreprocessingJS = new GetURL;

我希望得到以下结果:在viewDidLoad方法中,extensionContext?.inputItems将为我提供几个输入项,通过这些输入项,我可以获得所选的内容和Web URL.

I expected the following result: in viewDidLoad method extensionContext?.inputItems would provide me with several input items, through which I would be able to get the selected content and the web URL.

出了什么问题

viewDidLoad方法中,extensionContext?.inputItems仅向我提供一项-所选内容的纯文本表示形式(即使同时选择图像和文本也是如此).我可以使用纯文本,但是我需要网页URL.

In viewDidLoad method extensionContext?.inputItems provides me with only one item -- the plain text representation of the selected content (even when I selected images and text at the same time). I can live with plain text, but I need the webpage URL.

我的问题

在iOS Safari中使用共享扩展程序通过上下文菜单共享所选内容时,如何获取打开的网页的URL?

How can I get URL of the opened webpage when using a Share extension to share selected content via context menu in iOS Safari?

推荐答案

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

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.

这篇关于iOS共享扩展程序:通过Safari中的上下文菜单共享时获取页面的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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