0

I have files recorded by the user in the documents directory... displayed in a uitableview...

I placed this code in my didSelectRowAtIndexPath... Why doesn't this work?

     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];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
iDevMartin
  • 193
  • 3
  • 15
  • Have you stepped through your code in the debugger to make sure none of those references is nil? – matt Feb 03 '13 at 22:27
  • yes... the debugger is blank... is it supposed to be? – iDevMartin Feb 03 '13 at 22:29
  • So you don't know how to use the debugger? Learning to use it would be good! It shows you the values of all those references (paths, documentsDirectory, fileName, and so on) so you can see whether they are what you expect. Just walking through the code would probably have solved your problem. Otherwise you're sort of programming by guesswork... – matt Feb 04 '13 at 00:18

1 Answers1

1

You are creating the URL incorrectly. You need to use fileURLWithPath, not URLWithString.

Also, this:

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

Should be:

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

There is no need for the string format.

Last thing. Th file name passed to the addAttachment method should be just a file name, not a full pathname.

rmaddy
  • 314,917
  • 42
  • 532
  • 579