3

I've already figured out how to share the general content of a Note (text and pictures). But the way I currently approach the problem the markup of the note is not kept (Titles, Lists, etc.). I just receive the pure text of the note. When you share a note with Mail for instance, you can see that the markup is transferred. Is there a way to do that for your own apps?

My current solution where I only receive the pure text:

class ShareViewController: UIViewController{

    override func viewDidLoad() {

        if let content = extensionContext!.inputItems[0] as? NSExtensionItem {

            // Verify the provider is valid
            if let contents = content.attachments as? [NSItemProvider] {

                // look for images
                for attachment in contents {
                    print(attachment.registeredTypeIdentifiers)


                    if attachment.hasItemConformingToTypeIdentifier("public.plain-text"){
                        attachment.loadItem(forTypeIdentifier: "public.plain-text", options: nil) { data, error in

                            let string = data as! String
                            print(string)
                        }
                    }
                }
            }
        }
    }
}

EDIT:

My current NSExtensionActivationRules:

    <key>NSExtensionAttributes</key>
    <dict>
        <key>NSExtensionActivationRule</key>
        <dict>
            <key>NSExtensionActivationDictionaryVersion</key>
            <integer>2</integer>
            <key>NSExtensionActivationSupportsImageWithMaxCount</key>
            <integer>100</integer>
            <key>NSExtensionActivationSupportsText</key>
            <true/>
        </dict>
    </dict>
dbc
  • 104,963
  • 20
  • 228
  • 340
Christoph
  • 702
  • 6
  • 16

1 Answers1

1

Use the attributedContentText property of your NSExtensionItem (content):

override func viewDidLoad() {
    if let content = extensionContext!.inputItems[0] as? NSExtensionItem {
        // move your content validation to `func isContentValid() -> Bool`

        let attributedString = content.attributedContentText // yay NSAttributedString!
    }
}
d.felber
  • 5,288
  • 1
  • 21
  • 36
  • What you mentioned is great and it works but the attributes I'm looking for don't seem to be copied. The font-family and font-size are kept, but other formatting doesn't seem to be available (lists and their indent, header, etc.). I tried converting the `attributedString` into an html document and an rtf file, and this seems to not apply those attributes. Any ideas? – Christoph Nov 08 '16 at 14:01
  • @Christoph No sorry. If the attributes are not provided by the `attributedContentText` I'm afraid that there is no way to access those information. – d.felber Nov 08 '16 at 15:19
  • Ok, seems like it. Only thing i find weird is that when sharing a note with mail it is able to keep those attributes, but that seems to be an apple internal thing that we developers have no access to. If there aren't any other answers in the next couple of days, i will give you the bounty. Thanks for the help. :) – Christoph Nov 08 '16 at 16:41
  • 1
    @Christoph At least some basic bullet point formatting is kept. Maybe you can make use of that... but it's sad that the font size, weight, italic, underline info is missing :/ I would also be glad if someone has a better solution for this ;) – d.felber Nov 09 '16 at 07:31