Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何将保存的文本文件作为电子邮件附件发送_Iphone_Email_Xcode4_Ios4 - Fatal编程技术网

Iphone 如何将保存的文本文件作为电子邮件附件发送

Iphone 如何将保存的文本文件作为电子邮件附件发送,iphone,email,xcode4,ios4,Iphone,Email,Xcode4,Ios4,我有一个应用程序,它将通过电子邮件收集的数据作为附件发送。我使用以下代码创建数据并将其保存到文档文件夹中名为“leads.xml”的文件中 NSArray *sysPath = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES ); NSString *sysFilePath = [sysPath objectAtIndex:0]; [picker addAttachmentData:s

我有一个应用程序,它将通过电子邮件收集的数据作为附件发送。我使用以下代码创建数据并将其保存到文档文件夹中名为“leads.xml”的文件中

NSArray *sysPath = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *sysFilePath = [sysPath objectAtIndex:0];
[picker addAttachmentData:sysFilePath mimeType:@"text/xml" fileName:@"leads.xml"];

.
.
.

[emailBody writeToFile:filePath atomically:YES];
然后,我尝试使用以下代码将保存的文件作为附件发送-

NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/leads.xml", docDirectory];

当它运行时,我收到一个错误,我做错了什么?

邮件需要一个
NSData
对象,包含文件内容而不是路径。 您需要将文件加载到
NSData
对象中,如:

NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [docDirectory stringByAppendingPathComponent:@"leads.xml"];

NSData *fileData = [NSData dataWithContentsOfFile:filePath];
[picker addAttachmentData:fileData mimeType:@"text/xml" fileName:@"leads.xml"];

您必须提供数据,而不是文件路径:

NSData *data = [NSData dataWithContentsOfFile:sysFilePath];
[picker addAttachmentData:data mimeType:@"text/xml" fileName:fileName];
文件:

附件:

要附加的数据。通常,这是要包含的文件的内容。此参数不能为零。

请尝试此代码

if(mailController)
            {
                mailController.mailComposeDelegate = self;
               // NSLog(@"current index:%d",currentIndex_);
                NSString *string = [NSString stringWithFormat:@"00%d",currentIndex_+43];
              //  NSLog(@"string:%@",string);
                NSString *imageIndex = [[NSBundle mainBundle] pathForResource:string ofType:@"jpg"];

                NSData *myData = [[NSData alloc] initWithContentsOfFile:imageIndex];

                [mailController addAttachmentData:myData mimeType:@"application/jpg" fileName:@"Report"];
                [mailController setSubject:@"your subject"];
                [mailController setMessageBody:@"" isHTML:NO];
                [self presentModalViewController:mailController animated:YES];
                [mailController release];
            }
我在这里发送图像,而不是你可以发送你的文本文件。 这可能会帮助你做几件事

您可以在以下位置检查模拟器的文件系统:

~/Library/Application Support/iPhone Simulator/5.0/Applications/...
并查看您的文件是否实际保存在您期望的位置

第二件事:
NSString
实际上有很多方便的方法来处理文件路径,因此您不必检查后面的斜杠和其他垃圾。在处理文件路径时,应始终使用它们

使用路径

+ pathWithComponents:
– pathComponents
– completePathIntoString:caseSensitive:matchesIntoArray:filterTypes:
– fileSystemRepresentation
– getFileSystemRepresentation:maxLength:
– isAbsolutePath
– lastPathComponent
– pathExtension
– stringByAbbreviatingWithTildeInPath
– stringByAppendingPathComponent:
– stringByAppendingPathExtension:
– stringByDeletingLastPathComponent
– stringByDeletingPathExtension
– stringByExpandingTildeInPath
– stringByResolvingSymlinksInPath
– stringByStandardizingPath
– stringsByAppendingPaths:

问题不在于构建文件路径,而在于将文件数据添加到电子邮件中。您遇到的错误是。。??
+ pathWithComponents:
– pathComponents
– completePathIntoString:caseSensitive:matchesIntoArray:filterTypes:
– fileSystemRepresentation
– getFileSystemRepresentation:maxLength:
– isAbsolutePath
– lastPathComponent
– pathExtension
– stringByAbbreviatingWithTildeInPath
– stringByAppendingPathComponent:
– stringByAppendingPathExtension:
– stringByDeletingLastPathComponent
– stringByDeletingPathExtension
– stringByExpandingTildeInPath
– stringByResolvingSymlinksInPath
– stringByStandardizingPath
– stringsByAppendingPaths: