将 .mov 附加到 MFMailComposeViewController
Attaching .mov to MFMailComposeViewController
我想通过电子邮件发送.mov 文件。所以我正在使用 MFMailComposeViewController。花了一些时间搜索后,我终于写了下面的代码。
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];
当邮件编辑器出现时,我可以在邮件正文中看到附件,但我收到的这封邮件没有附件。
我是不是遗漏了什么?或者做错了什么?
请帮助我。
您没有正确获取文件数据。
第一步是拆分您的代码,使其更具可读性并且更容易调试。拆分此行:
[mail addAttachmentData:[NSData dataWithContentsOfURL:[NSURL URLWithString:myPathDocs]] mimeType:@"video/quicktime" fileName:[myPathDocs lastPathComponent]];
进入这些:
NSURL *fileURL = [NSURL URLWithString:myPathDocs];
NSData *fileData = [NSData dataWithContentsOfURL:fileURL];
[mail addAttachmentData:fileData mimeType:@"video/quicktime" fileName:[myPathDocs lastPathComponent]];
现在当你调试这段代码时你会发现fileData
是nil
。 fileURL
也将是 nil
(或至少是无效的 URL)。
更改此行:
NSURL *fileURL = [NSURL URLWithString:myPathDocs];
至:
NSURL *fileURL = [NSURL fileURLWithPath:myPathDocs];
您需要这样做,因为 myPathDocs
是文件路径,而不是 URL 字符串。
此外,您应该修复构建方式 myPathDocs
。而不是:
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:@"/Saved Video/%@",[player.contentURL lastPathComponent]]];
你应该这样做:
NSString *myPathDocs = [[documentsDirectory stringByAppendingPathComponent:@"Saved Video"] stringByAppendingPathComponent:[player.contentURL lastPathComponent]];
我想通过电子邮件发送.mov 文件。所以我正在使用 MFMailComposeViewController。花了一些时间搜索后,我终于写了下面的代码。
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];
当邮件编辑器出现时,我可以在邮件正文中看到附件,但我收到的这封邮件没有附件。
我是不是遗漏了什么?或者做错了什么?
请帮助我。
您没有正确获取文件数据。
第一步是拆分您的代码,使其更具可读性并且更容易调试。拆分此行:
[mail addAttachmentData:[NSData dataWithContentsOfURL:[NSURL URLWithString:myPathDocs]] mimeType:@"video/quicktime" fileName:[myPathDocs lastPathComponent]];
进入这些:
NSURL *fileURL = [NSURL URLWithString:myPathDocs];
NSData *fileData = [NSData dataWithContentsOfURL:fileURL];
[mail addAttachmentData:fileData mimeType:@"video/quicktime" fileName:[myPathDocs lastPathComponent]];
现在当你调试这段代码时你会发现fileData
是nil
。 fileURL
也将是 nil
(或至少是无效的 URL)。
更改此行:
NSURL *fileURL = [NSURL URLWithString:myPathDocs];
至:
NSURL *fileURL = [NSURL fileURLWithPath:myPathDocs];
您需要这样做,因为 myPathDocs
是文件路径,而不是 URL 字符串。
此外,您应该修复构建方式 myPathDocs
。而不是:
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:@"/Saved Video/%@",[player.contentURL lastPathComponent]]];
你应该这样做:
NSString *myPathDocs = [[documentsDirectory stringByAppendingPathComponent:@"Saved Video"] stringByAppendingPathComponent:[player.contentURL lastPathComponent]];