Question

I'm working with Xcode.

In my app I save some UIdocuments at that location

[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];

I'm searching for a way to share documents, my first option is by email.

Can I send those documents by email, as an attachment? Can I open then with another device with the same app?

Was it helpful?

Solution

You could do like the following.

Create a MFMailComposeViewController and use - (void)addAttachmentData:(NSData*)attachment mimeType:(NSString*)mimeType fileName:(NSString*)filename method to add your attachment.

For example.

MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
[mailVC setSubject:@"Shared documents"];
[mailVC setToRecipients:@[@"sample@example.com"]];
[mailVC setMessageBody:@"Here the docs I want to share" isHTML:NO];
[mailComposer addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"file.pdf"];

[mailVC setMailComposeDelegate:self];

[self presentViewController:mailVC animated:YES completion:nil];

where pdfData is of type NSData. So, you need to transform your document into a NSData.

From Apple doc.

addAttachmentData:mimeType:fileName:

This method attaches the specified data after the message body but before the user’s signature. You may attach multiple files (using different file names) but must do so prior to displaying the mail composition interface. Do not call this method after presenting the interface to the user.

About the second part of your question. Could you explain what type of document do you need to display?

In the meantime, take a look at Adding "Open In..." option to iOS app.

OTHER TIPS

To send any attachment you need to get the contents into an NSData object. If the document is on disk then this is simple. You just need the path or file URL to the document. Then you can create the NSData object using the path or URL.

If the receiver of the email has the same app and the app is setup to appear in the "Open In" menu for documents of this type, then the user can open the app from the attachment. Your app then just needs to know what to do when it is asked to open a file of this type. There are plenty of existing documentation and questions here on SO that describe how to register an app to open certain file types.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top