Ideal way to send PDF attachments via email in iOS app

It seems to me that I was looking for a watch, trying to find a solution, and nothing worked. Anyway, here's how I managed to send the PDF from the URL as an attachment via email in the iOS app.

-(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 can I address the error checking the returned data?

+4
source share
3 answers

Firstly, you did not check for errors there to handle any problems if the PDF data could not be downloaded. (The best solution would be to use the NSData method dataWithContentsOfURL:options:error: and actively check if any errors have occurred.)

Also, if the PDF data can be quite large, I will be tempted to use NSURLConnection to asynchronously download the PDF in the background before trying to create an email if possible.

+4
source

It is intended to verify that mail is enabled and configured before using it, but this does not have to be in this method.

If you refer to error checking of an inappropriate email message, I suspect that there are delegation methods for this. If the mail successfully sends a message to the server, but the OS of messages addressed to a nonexistent address, the address "from" should receive an email.

+1
source

I think it would be nice to mention this answer from another question here that provided some sample code for checking errors when sending mail:

0
source

Source: https://habr.com/ru/post/1341163/


All Articles