Search code examples
iosemailavaudioplayeremail-attachmentsmfmailcomposeviewcontroller

ios , Attach Audio file aac format to Email


i try to send a audio record by file aac (kAudioFormatMPEG4AAC) format but the file attached it's doesn't send

*myString = file://localhost/private/var/mobile.......

here is my code

MFMailComposeViewController *picker =
[[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"My Audio File"];  
NSString *fileName = @"Rec.aac";  

NSString *documentsDirectory = myString ;

NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];
NSData   *data = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:data mimeType:@"audio/aac"
                 fileName:fileName];
NSString *emailBody = @"AAC format sound file attached.";  
[picker setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:picker animated:YES];
[picker release];

Solution

  • NSData   *data = [NSData dataWithContentsOfFile:path];
    

    You're passing in a file:// URL, which won't be understood by that method. That method expects just a standard file path, i.e. /private/var/mobile/.

    You could simply use the file path, but if you're only provided with a URL form string, you can create a URL object and use that with NSData.

    NSURL* fileURL = [NSURL URLwithString:myString];
    NSError* error = nil;
    NSData* data = [NSData dataWithContentsOfURL:fileURL options:0 error:&error];
    
    if (error) {
        NSLog(@"Unable to load file from provided URL %@: %@", myString, error);
    }