PDF文件没有附加到邮件编辑器

时间:2014-07-19 18:14:15

标签: objective-c email pdf

我正在使用 MFMailComposeViewController 创建一个附加到电子邮件的pdf文件,但我在执行附加之前检查文件并且为空(nil)当然没有发送任何内容用电子邮件。

我不知道什么是错的,因为我一步一步地按照我在这里和其他页面看到的那样,但仍然无法正常工作。

我错过了一些显然我没见过的东西..

这是邮件发送功能..

- (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];


}

我已使用所有这些表单获取文件路径和类型,然后将其附加到我的电子邮件编辑器...

// Attach a pdf to the email
//NSString *path = [[NSBundle mainBundle] pathForResource:@"invoce" ofType:@"pdf"];

//NSData *myData = [NSData dataWithContentsOfFile:path];

//NSData *pdfData = [NSData dataWithContentsOfURL:[[[NSBundle mainBundle] resourceURL] URLByAppendingPathComponent:pdfFileName]];
//[mailView addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"invoce"];

// Get the resource path and read the file using NSData
//NSString *filePath = [[NSBundle mainBundle] pathForResource:filename ofType:extension];
//NSData *fileData = [NSData dataWithContentsOfFile:filePath];

//NSMutableData *pdfData = [NSMutableData data];
//UIGraphicsBeginPDFContextToData(pdfData, bounds, nil);

然后,这是我创建pdf文件的功能,我使用this example/tutorial

-(void)drawText
{
    fileName = @"Invoice.PDF"; // NSString

    // NSArray
    arrayPaths =
    NSSearchPathForDirectoriesInDomains(
                                        NSDocumentDirectory,
                                        NSUserDomainMask,
                                        YES);

    // NSString
    path = [arrayPaths objectAtIndex:0];

    // NSString
    pdfFileName = [path stringByAppendingPathComponent:fileName];

    NSString* textToDraw = @"Hello World";
    CFStringRef stringRef = (__bridge CFStringRef)textToDraw;

    // Prepare the text using a Core Text Framesetter.
    CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL);
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);

    CGRect frameRect = CGRectMake(0, 0, 300, 50);
    CGMutablePathRef framePath = CGPathCreateMutable();
    CGPathAddRect(framePath, NULL, frameRect);

    // Get the frame that will do the rendering.
    CFRange currentRange = CFRangeMake(0, 0);
    CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
    CGPathRelease(framePath);

    // Create the PDF context using the default page size of 612 x 792.
    UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);

    // Mark the beginning of a new page.
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);

    // Get the graphics context.
    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    // Put the text matrix into a known state. This ensures
    // that no old scaling factors are left in place.
    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);

    // Core Text draws from the bottom-left corner up, so flip
    // the current transform prior to drawing.
    CGContextTranslateCTM(currentContext, 0, 100);
    CGContextScaleCTM(currentContext, 1.0, -1.0);

    // Draw the frame.
    CTFrameDraw(frameRef, currentContext);

    CFRelease(frameRef);
    CFRelease(stringRef);
    CFRelease(framesetter);

    // Close the PDF context and write the contents out.
    UIGraphicsEndPDFContext();

}

不确定您是否需要更多解释或信息,请告知我们,我会在帖子中发表评论或提供..

2 个答案:

答案 0 :(得分:0)

你的drawText()方法正在创建带有文本&#34; hello world&#34;内部文件夹。 要手动检查这个,您可以找到此文件的路径,尝试使用以下代码行来记录此路径。

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];

答案 1 :(得分:0)

我最终使用this post来帮助我解决问题..

换句话说,这就是我在 emailSend 函数中所做的..

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"];

感谢您的帮助:)