Ideal means of sending a PDF attachment through in-app email on iOS
开发者_运维知识库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.
You are supposed to check that mail is enabled and configure before using it, but that doesnt need to be in this method.
If you are referring to error checking a failed email, I suspect that there are delegate methods for that. If mail successfully sends the message to a server, but the message OS addressed to a nonexistent address, the "from" address should receive an email.
I think it would be nice to mention this answer from another question here, that has provided some sample code for error checking on sending mail:
精彩评论