繁体   English   中英

在iOS 5中的Objective-C中未将pdf添加到电子邮件中

[英]pdf not added to email in objective-c in IOS 5

我正在尝试将电子邮件添加为邮件的附件。 我是这样的

-(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中不起作用

首先要显示视图控制器,然后再附加文件。 更改操作顺序:

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

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

希望它能解决问题:)

尝试这个,

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM