I am super new to Objective-C, so please help me out.
I have a view with several labels and text views. I am trying to generate a PDF file for this page / view and connect to the email.
Firstly, I tried this approach, and it seems that the file has not been created or is not attached to the letter. Here are my codes:
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, self.view.bounds, nil);
UIGraphicsBeginPDFPage();
[self.view drawRect:self.view.bounds];
UIGraphicsEndPDFContext();
MFMailComposeViewController *mailComposer = [[[MFMailComposeViewController alloc] init] autorelease];
mailComposer.mailComposeDelegate = self;
[pdfData writeToFile:file atomically:YES];
[mailComposer addAttachmentData:pdfData mimeType:@"application/pdf" fileName:file];
[self presentModalViewController:mailComposer animated:YES];
And since this did not work, I tried a different approach, first creating a PDF and then attaching it to the email. Then I saw that the PDF file was created in this directory, but it is empty !!!
Here are my revised codes:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [documentsDirectory stringByAppendingFormat:@"/tempFile.pdf"];
UIGraphicsBeginPDFContextToFile(file, self.view.bounds, nil);
UIGraphicsBeginPDFPage();
[self.view drawRect:self.view.bounds];
UIGraphicsEndPDFContext();
MFMailComposeViewController *mailComposer = [[[MFMailComposeViewController alloc] init] autorelease];
mailComposer.mailComposeDelegate = self;
[pdfData writeToFile:file atomically:YES];
[mailComposer addAttachmentData:pdfData mimeType:@"application/pdf" fileName:file];
[self presentModalViewController:mailComposer animated:YES];
- Can you please help me successfully create a PDF file from the view and then attach it to the letter?
I tried several ways, but I canβt understand what I am doing wrong. Many thanks!