问题
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.
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.
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>
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.
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}")
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
is something like this (I've changed the id string in the middle):
https://www.dropbox.com/s/lskd8jejbj8wpo/testfile.text?dl=0
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?
Note: If I share the file to Evernote or TapForms it goes right in so it's not something about the file.
回答1:
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
could be changed to:
https://www.dropbox.com/s/lskd8jejbj8wpo/testfile.text?dl=1
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
来源:https://stackoverflow.com/questions/28311392/action-extension-trying-to-retrieve-a-file