Attaching .mov to MFMailComposeViewController

I want to email a .mov file. Therefore I am using MFMailComposeViewController. After spending some time searching, I finally wrote below code.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:
                                     [NSString stringWithFormat:@"/Saved Video/%@",[player.contentURL lastPathComponent]]];
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
[mail addAttachmentData:[NSData dataWithContentsOfURL:[NSURL URLWithString:myPathDocs]] mimeType:@"video/quicktime" fileName:[myPathDocs lastPathComponent]];
[mail setSubject:@"Share VIDEO by My App"];
[self presentViewController:mail animated:YES completion:nil]; 

      

When the composer of the mail appears, I see the attachment in the body of the email, but I receive this email without the attachment.
Did I miss something? or is there something wrong?

Please help me.

+3


source to share


1 answer


You are not getting the data correctly for the file.

The first step is to split up the code to make it more readable and much easier to debug. Strip this line:

[mail addAttachmentData:[NSData dataWithContentsOfURL:[NSURL URLWithString:myPathDocs]] mimeType:@"video/quicktime" fileName:[myPathDocs lastPathComponent]];

      

in them:

NSURL *fileURL = [NSURL URLWithString:myPathDocs];
NSData *fileData = [NSData dataWithContentsOfURL:fileURL];
[mail addAttachmentData:fileData mimeType:@"video/quicktime" fileName:[myPathDocs lastPathComponent]];

      

Now when you debug this code, you will find that fileData

- nil

. fileURL

will also be nil

(or at least a bad url).

Change this line:

NSURL *fileURL = [NSURL URLWithString:myPathDocs];

      



in

NSURL *fileURL = [NSURL fileURLWithPath:myPathDocs];

      

You need to do this since is myPathDocs

is a file path, not a URL string.

Also, you have to fix how you create myPathDocs

. Instead:

NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:
                             [NSString stringWithFormat:@"/Saved Video/%@",[player.contentURL lastPathComponent]]];

      

You should do something like:

NSString *myPathDocs = [[documentsDirectory stringByAppendingPathComponent:@"Saved Video"] stringByAppendingPathComponent:[player.contentURL lastPathComponent]];

      

+4


source







All Articles