1

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
KGS-12
  • 537
  • 4
  • 7
  • Did you try my code? Cheers. – Lorenzo B Jun 25 '13 at 21:27
  • throw that error...Undefined symbols for architecture armv7: "_OBJC_CLASS_$_MFMailComposeViewController", referenced from: objc-class-ref in DropsViewController.o ld: symbol(s) not found for architecture armv7 clang: error: linker command failed with exit code 1 (use -v to see invocation) – KGS-12 Jun 25 '13 at 23:18
  • You need to import the framework for this. It is called **MessageUI.framework**. Follow step 3 within this tutorial. http://mobile.tutsplus.com/tutorials/iphone/mfmailcomposeviewcontroller/ – Lorenzo B Jun 26 '13 at 09:36
  • I solved the problem :)... I use dataWithContentsOfFile to get NSData for add attachment `NSData *myData = [NSData dataWithContentsOfFile: path options: 0 error: &error];` Now it is throwing an error "The operation couldn’t be completed. Permission denied" . File is at NSFilePath=/private/var/mobile/Applications/E296F71E-25FF-4B9E-A9FF-9E6D74D7D63A/Documents/File.drp . How can I give permission? where can I move file ? – KGS-12 Jun 26 '13 at 16:11
  • When do you receive the error? – Lorenzo B Jun 26 '13 at 16:13
  • `NSData *myData = [NSData dataWithContentsOfFile: path options: 0 error: &error];` at that line NSData is NULL,then I print error: Printing description of error: Error Domain=NSCocoaErrorDomain Code=257 "The operation couldn’t be completed. (Cocoa error 257.)" UserInfo=0x1e0c3cb0 {NSFilePath=/private/var/mobile/Applications/E296F71E-25FF-4B9E-A9FF-9E6D74D7D63A/Documents/File.drp, NSUnderlyingError=0x1e0c3b70 "The operation couldn’t be completed. Permission denied"} – KGS-12 Jun 26 '13 at 16:32
  • I solved it by sending a archive, not an UIDocument. – KGS-12 Jun 26 '13 at 18:28

2 Answers2

2

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.

Community
  • 1
  • 1
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
0

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579