动作扩展尝试检索文件 [英] Action Extension Trying to Retrieve a File

查看:90
本文介绍了动作扩展尝试检索文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在iOS 8中创建Action Extension.从一个新项目开始,我创建了一个单一视图应用程序,并为Action Extension添加了新目标.默认的Action Extension模板配置为显示图像.当我从照片"共享时,图像显示在扩展程序的视图控制器上,因此基本管道正常工作.

I'm trying to create an Action Extension in iOS 8. Starting with a new project I created a single view application and added a new target for the Action Extension. The default Action Extension template is configured to display an image. When I share from Photos the image shows up on the view controller for the extension so the basic plumbing is all working.

真正的用例是我想从Dropbox(或Air Sharing或其他)共享文本文件到应用程序,并让应用程序处理该文件.

The real use case is that I want to share a text file from Dropbox (or Air Sharing, or whatever) to the app and have the app process the file.

首先,我更改了info.plist:

First I changed info.plist:

<key>NSExtension</key>
<dict>
    <key>NSExtensionAttributes</key>
    <dict>
        <key>NSExtensionActivationRule</key>
        <dict>
            <key>NSExtensionActivationSupportsFileWithMaxCount</key>
            <string>1</string>
            <key>NSExtensionActivationSupportsText</key>
            <string>1</string>
        </dict>
    </dict>
    <key>NSExtensionMainStoryboard</key>
    <string>MainInterface</string>
    <key>NSExtensionPointIdentifier</key>
    <string>com.apple.ui-services</string>
</dict>

我在Dropbox中添加了testfile.text,当我在Dropbox应用中导航至它并点击共享"按钮时,出现了我的扩展名,因此激活规则似乎有效.

I added testfile.text to Dropbox and when I navigate to it in the Dropbox app and tap the share button, my extension appears so the activation rules seem to be working.

当我登录扩展上下文时,这就是我从self.extensionContext.inputItems获得的内容:

When I log the extension context this is what I get for self.extensionContext.inputItems:

self.extensionContext.inputItems= (
    "<NSExtensionItem: 0x15657180> - userInfo: {\nNSExtensionItemAttachmentsKey =     (\n        \"<NSItemProvider: 0x15658c30> {types = (\\n    \\\"public.url\\\"\\n)}\"\n    );\n}")

有一个类型为public.url的项目提供者.所以我像这样在viewDidLoad中修改了模板代码,以查找kUTTypeURL类型:

There's one item provider with a type of public.url. So I modified the template code in viewDidLoad like this to look for type kUTTypeURL:

for (NSExtensionItem *item in self.extensionContext.inputItems) {
    for (NSItemProvider *itemProvider in item.attachments) {
        if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeURL]) {
            __weak UITextView *textView = self.textView;
            [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeURL options:nil completionHandler:^(NSURL *url, NSError *error) {
                if(url) {
                    NSString *text = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; // just using this to test
                    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                        [textView setText:text];
                    }];

url就像这样(我在中间更改了id字符串):

url is something like this (I've changed the id string in the middle):

https://www.dropbox.com/s/lskd8jejbj8wpo/testfile.text?dl=0

initWithContentsOfURL之后,我收到了文本,但这不是我想要的.测试文件大约为300个字节.返回的结果是大约70,000字节的元数据.

After initWithContentsOfURL I get text but it's not what I want. The test file is about 300 bytes. What comes back is about 70,000 bytes of meta data.

如何获取文件?我使用了错误的类型标识符吗?

How do I get the file? Am I using the wrong type identifier?

注意:如果我将文件共享到Evernote或TapForms,它将直接进入,因此与文件无关.

Note: If I share the file to Evernote or TapForms it goes right in so it's not something about the file.

推荐答案

您返回的URL类型是用于与人类共享的,因此它返回一个预览页面,用户可以在其中选择下载文件,等等.如果您需要以编程方式下载原始文件内容,则需要首先修改链接的参数.例如,您的假设示例:

The type of URL you're getting back is meant for sharing to humans, and so it returns a preview page where a user can choose to download the file, etc. If you instead need to programmatically download the original file content, you'll need to modify the link's parameters first. For example, your hypothetical example:

https://www.dropbox.com/s/lskd8jejbj8wpo/testfile.text?dl = 0

可以更改为:

https://www.dropbox.com/s/lskd8jejbj8wpo/testfile.text?dl = 1

只要您的代码遵循URL返回的重定向,就可以下载文件内容.

That would let you download the file content, as long as your code follows redirects returned by the URL.

有关此的更多信息,请参阅此帮助文章:

For more information on this, refer to this help article:

https://www.dropbox.com/help/201

这篇关于动作扩展尝试检索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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