[英]how to email file from documents directory of iphone app
I am saving file documents directory after that i want to send that file in email but problem is that it doest not get attached i think problem is due path conflict or anything else here is the code where i save file. 我要保存文件文档目录之后,我想在电子邮件中发送该文件,但问题是它没有被连接,我认为问题是由于路径冲突或其他任何原因,这是我保存文件的代码。
// Retrieves the document directories from the iOS device //从iOS设备检索文档目录
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];
// instructs the mutable data object to write its context to a file on disk //指示可变数据对象将其上下文写入磁盘上的文件
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);
and here is the code which i am using for sending this file in email 这是我用于通过电子邮件发送此文件的代码
- (NSString *)pathForFile : (NSString *) fileName{
return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: fileName];
}
- (void) sendMailWithAttachedFile : (NSString *) fileName :(NSString *) extension{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
// NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:[self pathForResourse:fileName ofType:extension]];
NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:[self pathForFile:[NSString stringWithFormat:@"%@.%@", fileName, extension]]];
NSData *data=[[NSData alloc]initWithContentsOfURL:outputURL];
[picker addAttachmentData:data mimeType:@"documents/pdf" fileName:@"TestOne.pdf"];
[self presentModalViewController:picker animated:YES];
}
-(IBAction)onEmailResultPDF{
[self sendMailWithAttachedFile:@"TestOne":@"pdf"];
}
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSString *fileName = [[NSString alloc]initWithFormat:@"%@.pdf",giveFileName];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
NSMutableData *myPdfData = [NSMutableData dataWithContentsOfFile:pdfFileName];
[picker addAttachmentData:myPdfData mimeType:@"application/pdf" fileName:giveFileName];
[self.navigationController presentViewController:picker animated:YES completion:nil];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.