附件不是通过ipad的电子邮件发送的

时间:2013-01-18 23:46:00

标签: xcode ipad ios6

我有一台iPad,它有一个例程来创建一个pdf并作为电子邮件的附件发送。这一切似乎与电子邮件作曲家开放一起显示附加的pdf文档。但是,在iPad上测试时,收到电子邮件时没有附件。有什么想法吗?

    [mailComposer addAttachmentData:data mimeType:@"application/pdf" fileName:@"pdffile.pdf"];
    [self presentViewController:mailComposer animated:YES completion:nil];

非常感谢

详情:

创建pdf文件并将其命名为pdffile.pdf。以下是完整的电子邮件例程:

    MFMailComposeViewController *mailComposer;
    mailComposer  = [[MFMailComposeViewController alloc] init];
    mailComposer.mailComposeDelegate = self;
    [mailComposer setModalPresentationStyle:UIModalPresentationFormSheet];
    [mailComposer setSubject:[NSString stringWithFormat: @"i-observe Lesson Observation for: %s", "date"]];
    [mailComposer setMessageBody:[NSString stringWithFormat: @"i-observe Lesson Observation for: %s", "name"] isHTML:NO];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *file = [documentsDirectory stringByAppendingFormat:@"pdffile.pdf"];
    NSMutableData *data=[NSMutableData dataWithContentsOfFile:file];
    [mailComposer addAttachmentData:data mimeType:@"application/pdf" fileName:@"pdffile.pdf"];
    [self presentViewController:mailComposer animated:YES completion:nil];

3 个答案:

答案 0 :(得分:2)

尝试此方法:

 if([MFMailComposeViewController canSendMail]){      

        MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
        mail.mailComposeDelegate=self;
        [mail setSubject:@"Email with attached pdf"];   
        NSString *newFilePath = @"get path where the pdf reside";

        NSData * pdfData = [NSData dataWithContentsOfFile:newFilePath];
    [mail addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"yourpdfname.pdf"];
        NSString * body = @"";
        [mail setMessageBody:body isHTML:NO];
        [self presentModalViewController:mail animated:YES];
        [mail release];         
    }
    else
    {
        NSLog(@"Message cannot be sent");
    }

答案 1 :(得分:0)

下一个解决方案是假设pdf文件位于主包中:

 NSBundle *mainBundle = [NSBundle mainBundle];
        NSString *myFile = [mainBundle pathForResource: @"RealNameofFile" ofType: @"pdf"];
        NSData *pdfD = [NSData dataWithContentsOfFile:myFile];
        [mailViewController addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"Nametodisplayattached.pdf"];

答案 2 :(得分:0)

确保该文件与表格单元格的名称相同