I feel like I looked for hours trying to find a solution and nothing worked. Anyway, here’s how I’ve managed to send a PDF from a URL as an attachment through an in-app email on iOS.
-(void)emailDocument:(id)sender {
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
NSString *emailSubject = [NSString localizedStringWithFormat:@"Hi, I'm the subject"];
[controller setSubject:emailSubject];
NSString *path = @"http://www.somesite.com/document.pdf";
NSURL *pdfURL = [NSURL URLWithString:path];
NSData *pdfData = [NSData dataWithContentsOfURL:pdfURL];
[controller addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"documentname.pdf"];
//[controller setToRecipients:[NSArray arrayWithObject:[NSString stringWithString:@"YourEmail@me.com"]]];
//[controller setMessageBody:@"Custom messgae Here..." isHTML:NO];
[self presentModalViewController:controller animated:YES];
controller.mailComposeDelegate = self;
[controller release];
}
So my question is, are there any potential problems with this? And how would I go about error checking the data coming back?
First off, you’ve put no error checking in there to handle any issues if the PDF data couldn’t be loaded. (A better solution would be to use the NSData
dataWithContentsOfURL:options:error:method and actively check to see if any errors occurred.)Additionally, if the PDF data is liable to be quite large, I’d be tempted to use an NSURLConnection to asynchronously load the PDF data in the background, prior to attempting to create the email if this is feasible.