Objective c PDF文件未附加到邮件生成器
我正在创建一个pdf文件,以便使用MFMailComposeViewController将其附加到电子邮件中,但在执行附加之前,我正在检查该文件,并且该文件为空,没有任何内容会随电子邮件一起发送 我不知道出了什么问题,因为我正在一步一步地遵循我在这里和其他页面中看到的内容,但仍然不起作用 我错过了一些我显然看不见的东西 这是邮件发送功能Objective c PDF文件未附加到邮件生成器,objective-c,email,pdf,Objective C,Email,Pdf,我正在创建一个pdf文件,以便使用MFMailComposeViewController将其附加到电子邮件中,但在执行附加之前,我正在检查该文件,并且该文件为空,没有任何内容会随电子邮件一起发送 我不知道出了什么问题,因为我正在一步一步地遵循我在这里和其他页面中看到的内容,但仍然不起作用 我错过了一些我显然看不见的东西 这是邮件发送功能 - (IBAction)emailSend:(UIButton *)sender { // calling pdf file creator
- (IBAction)emailSend:(UIButton *)sender {
// calling pdf file creator
[self drawText];
// Email message implementation
// Email Subject
NSString *emailTitle = myMssgSubject.text;
// Email Content
NSString *messageBody = [NSString stringWithFormat:@"<h3>%@</h3>\n<h4>iReceipt Number: %@</h4>\n<h4>Customer: %@</h4>\n<h4>Customer Address: %@</h4>\n<h4>Customer Phone: %@</h4>", myMssgText.text, myReceiptNo, myCustomer, myCusAddress, myCusPhoneNo]; // Change the message body to HTML
// To address
NSArray *toRecipents = [NSArray arrayWithObject:myMssgReceiver.text];
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:emailTitle];
[mailViewController setMessageBody:messageBody isHTML:YES];
[mailViewController setToRecipients:toRecipents];
NSData *pdfData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"invoice" ofType:@"pdf"]];
if(pdfData == nil)
NSLog(@"Its empty!");
// for .doc is application/msword and for .docx is application/vnd.openxmlformats-officedocument.wordprocessingml.document
[mailViewController addAttachmentData:pdfData mimeType:@"application/pdf" fileName:fileName];
// Present mail view controller on screen
[self presentViewController:mailViewController animated:YES completion:NULL];
}
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
NSString* fileName = @"invoice.pdf";
NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [arrayPaths objectAtIndex:0];
NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];
NSData *myData = [NSData dataWithContentsOfFile:pdfFileName];
[mailComposer addAttachmentData:myData mimeType:@"application/pdf" fileName:@"invoice.pdf"];
然后,这是我创建我正在使用的pdf文件的函数
不确定你们是否需要更多的解释或信息,请让我知道,我会在帖子中评论或提供 您的drawText方法正在使用documents文件夹中的文本hello world创建文件。 若要手动签出此文件,您可以找到此文件的路径,请尝试使用以下代码行记录此路径
NSString *appFolderPath = [[NSBundle mainBundle] resourcePath];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSLog(@"App Directory is: %@", appFolderPath);
所以,如果你不能把这个文件和附件的电子邮件,这是第二个问题。一切都好
NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:@"Invoice.pdf"];
NSData *pdfData = [NSData dataWithContentsOfFile:fullPath];
我最后用它来解决问题
换句话说,这就是我在emailSend功能中所做的
- (IBAction)emailSend:(UIButton *)sender {
// calling pdf file creator
[self drawText];
// Email message implementation
// Email Subject
NSString *emailTitle = myMssgSubject.text;
// Email Content
NSString *messageBody = [NSString stringWithFormat:@"<h3>%@</h3>\n<h4>iReceipt Number: %@</h4>\n<h4>Customer: %@</h4>\n<h4>Customer Address: %@</h4>\n<h4>Customer Phone: %@</h4>", myMssgText.text, myReceiptNo, myCustomer, myCusAddress, myCusPhoneNo]; // Change the message body to HTML
// To address
NSArray *toRecipents = [NSArray arrayWithObject:myMssgReceiver.text];
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:emailTitle];
[mailViewController setMessageBody:messageBody isHTML:YES];
[mailViewController setToRecipients:toRecipents];
NSData *pdfData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"invoice" ofType:@"pdf"]];
if(pdfData == nil)
NSLog(@"Its empty!");
// for .doc is application/msword and for .docx is application/vnd.openxmlformats-officedocument.wordprocessingml.document
[mailViewController addAttachmentData:pdfData mimeType:@"application/pdf" fileName:fileName];
// Present mail view controller on screen
[self presentViewController:mailViewController animated:YES completion:NULL];
}
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
NSString* fileName = @"invoice.pdf";
NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [arrayPaths objectAtIndex:0];
NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];
NSData *myData = [NSData dataWithContentsOfFile:pdfFileName];
[mailComposer addAttachmentData:myData mimeType:@"application/pdf" fileName:@"invoice.pdf"];
谢谢你的帮助:结帐我以前试过,但不起作用,我想是少了些什么,但现在是。。谢谢对不起,我真的不明白你想做什么。。路径从何而来??fileManager也从未使用过。。