How do I export text from iOS Notes app to my app?

2020-05-06 13:49发布

问题:

I know this is possible and is something many big apps do, but I can't figure out among UTIs, extensions, UIActivityTypes, etc. which spell I need to use for this in particular. I'm trying to add my app to the list of apps that appear when you hit the export button in the Notes app on iOS because I want to be able to take a bunch of text inside a note and repeatedly and safely send it into my app.

I want my app to appear in this list, exporting directly from Notes. There appear to be duplicates of this question that are very old claiming that "exporting from Notes app is not possible for third party apps", but I think they're either now wrong or answering a different question because as you can see in the above screenshot, Messenger is plainly able to do this.

It does not look like registering UTIs gets this done for me. I went ahead and registered my app to all sorts of UTIs, but all that happens is my app ends up in popups like this one, which appears when trying to open a txt file, which is not what I'm looking for. Unless there is some other UTI that does not conform to public.data or public.content which I need to be using, in which case I'd be happy to hear it.

回答1:

I used one of my allotted support requests to ask Apple, and after some time and back and forth I was able to get my answer! Here is what they said:

Your project is missing a Share app extension. Start by reviewing the App Extension Programming Guide: https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/index.html#//apple_ref/doc/uid/TP40014214-CH20-SW1

Information specific to the Share extension point: https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/Share.html#//apple_ref/doc/uid/TP40014214-CH12-SW1

You don’t need to register the text content type in your CFBundleDocumentTypes. as you’ve done in your sample. However, you will need to provide an activation rule for the extension point, such as the one shown in this section of the document (the section titled Declaring Supported Data Types for a Share or Action Extension): https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW1

See specifically the predicate example that activates the extension if an attachment with the com.adobe.pdf UTI is present. You will need to do something similar, to cover the UTIs for content you would like your share extension to consume. As a starting point, you can consume text with the public.text UTI. For more common UTIs, please see this document: https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html#//apple_ref/doc/uid/TP40009259-SW1

I also found the bits about providing an activation rule to be super confusing, and this answer helped that: How do I set NSExtensionActivationRule predicates?