【问题标题】:attachment not sent with email from ipad附件未通过 ipad 的电子邮件发送
【发布时间】:2013-01-02 18:51:55
【问题描述】:

我有一台 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];

【问题讨论】:

  • 我们能看到1)mailCompose的初始化2)数据的初始化

标签: xcode ipad ios6


【解决方案1】:

试试这个方法:

 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");
    }

【讨论】:

  • 我有几天无法连接到 iPad。我会尽快通知你。
  • 感谢您的帮助。然而,这不是问题。就是那个 stringByAppendingFormat:@"pdffile.pdf"];应该是 stringByAppendingFormat:@"/pdffile.pdf"];还是谢谢。
【解决方案2】:

下一个解决方案是假设 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"];

【讨论】:

    【解决方案3】:

    确保文件与表格单元格同名

    【讨论】:

    • 这更像是一个“评论”而不是一个答案。首先,一个答案已经被接受,除非你认为你有更好的答案,否则你不应该发布一个。如您所见,接受的答案包含有关如何解决问题的完整说明和代码。这只是一个建议,背后没有太多的推理等。我知道你还不能在这个网站上发布 cmets,但是如果你有什么东西应该在评论中,不是答案,也不是非常重要,那就不要发布任何东西。
    猜你喜欢
    • 1970-01-01
    • 2015-10-23
    • 2015-09-30
    • 2015-07-23
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    相关资源
    最近更新 更多