0

我正在尝试将电子邮件作为附件添加到我的邮件中。我这样做。

-(IBAction)mailPDF:(id)sender{
    MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
    controller.mailComposeDelegate = self;
    NSLog(@"myData is %@",myData);
    [controller setSubject:@"Geselecteerde favorieten van Genk on Stage"];
    [controller setMessageBody:@"<p>Hallo muziekliefhebber <br /> In bijlage vind je jouw favorieten. Volg en praat met ons mee op facebook.com/genkonstage of @genkonstage!<br /> Veel plezier op Genk on stage! </p>" isHTML:YES];
    if (controller){
        [self presentModalViewController:controller animated:YES];
        [controller addAttachmentData:myData mimeType:@"application/pdf" fileName:@"favorite.pdf"];
    }else{
        NSLog(@"nothing to show");
    }
}

这就是我设置 myData 的方式

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"favorite.pdf"];
 myData = [NSData dataWithContentsOfFile: filePath];

当我查看我的日志myData(这是我的 pdf)时,它不是空的。此外,当我在查找器中浏览到我的文档文件夹时,simulator我看到我有一个 PDF。

有人能告诉我为什么我的 pdf 没有添加到我的邮件中吗?

谢谢你!

编辑 上面的代码似乎只适用于 IOS6。所以现在的问题是。为什么它在 IOS 5 中不起作用

4

2 回答 2

1

您首先呈现视图控制器,然后附加文件。改变动作的顺序:

也就是说,在您的代码行中:

[self presentModalViewController:controller animated:YES];

[controller addAttachmentData:myData mimeType:@"application/pdf" fileName:@"favorite.pdf"];

将它们修改为:

[controller addAttachmentData:myData mimeType:@"application/pdf" fileName:@"favorite.pdf"];

[self presentModalViewController:controller animated:YES];

希望它能解决问题:)

于 2013-06-13T12:36:39.767 回答
0

尝试这个,

-(IBAction)mailPDF:(id)sender{
  MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
  controller.mailComposeDelegate = self;
  NSLog(@"myData is %@",myData);
  [controller setSubject:@"Geselecteerde favorieten van Genk on Stage"];
  [controller setMessageBody:@"<p>Hallo muziekliefhebber <br /> In bijlage vind je jouw favorieten. Volg en praat met ons mee op facebook.com/genkonstage of @genkonstage!<br /> Veel plezier op Genk on stage! </p>" isHTML:YES];

  if (controller)
     {
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"favorite.pdf"];
     NSData *myData = [NSData dataWithContentsOfFile:filePath];
     [controller addAttachmentData:myData mimeType:@"application/pdf" fileName:@"favorite.pdf"];
     [self presentModalViewController:controller animated:YES];
    }
 else{
    NSLog(@"nothing to show");
    }
}
于 2013-06-13T12:38:13.543 回答