- Enumerate the attachments. [NSTextStorage enumerateAttribute:...]
- Get the attachment's filewrapper.
Write to a URL.
[textStorage enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, textStorage.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) { NSTextAttachment* attachment = (NSTextAttachment*)value; NSFileWrapper* attachmentWrapper = attachment.fileWrapper; [attachmentWrapper writeToURL:outputURL options:NSFileWrapperWritingAtomic originalContentsURL:nil error:nil]; (*stop) = YES; // stop so we only write the first attachment }];
This sample code will only write the first attachment to outputURL.