【问题标题】:Attaching a recorded .caf file locatted in documents directory to mail iOS Xcode将位于文档目录中的记录的 .caf 文件附加到邮件 iOS Xcode
【发布时间】:2013-02-03 22:25:14
【问题描述】:

我在文档目录中有用户记录的文件...显示在 uitableview...

我将此代码放在我的 didSelectRowAtIndexPath 中...为什么这不起作用?

     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",[directoryContents objectAtIndex:indexPath.row]]];
NSURL *audioURL = [NSURL URLWithString:fileName];
NSData *audioData = [NSData dataWithContentsOfURL:audioURL];

MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];

    mailer.mailComposeDelegate = self;

[mailer addAttachmentData:audioData mimeType:@"audio/caf" fileName:fileName];

【问题讨论】:

  • 你有没有在调试器中单步调试你的代码以确保这些引用都不为零?
  • 是的......调试器是空白的......它应该是?
  • 所以你不知道调试器怎么用?学习使用它会很好!它向您显示所有这些引用(路径、documentsDirectory、fileName 等)的值,以便您查看它们是否符合您的预期。只需浏览代码可能就可以解决您的问题。否则你就是在猜测编程......

标签: ios email documents mfmailcomposer caf


【解决方案1】:

您创建的 URL 不正确。您需要使用fileURLWithPath,而不是URLWithString

还有,这个:

NSString *fileName = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",[directoryContents objectAtIndex:indexPath.row]]];

应该是:

NSString *fileName = [documentsDirectory stringByAppendingPathComponent:[directoryContents objectAtIndex:indexPath.row]];

不需要字符串格式。

最后一件事。传递给addAttachment 方法的文件名应该只是一个文件名,而不是完整的路径名。

【讨论】:

  • 然后我得到一个错误:ARC语义问题...没有已知的选择器'fileURLWithString:'的类方法
  • 对不起,错字。那应该是 fileURLWithPath。
  • 非常感谢您的快速回复!非常感谢!
猜你喜欢
  • 2016-09-16
  • 2015-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多