0
我對IoS比較陌生,在使用郵件附件時遇到問題。以下是我的代碼。將字符串保存爲具有自定義名稱的文件附件IOS
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt",
documentsDirectory];
//create content - four lines of text
NSString *content = @"One\nTwo\nThree\nF\nFive";
//save content to the documents directory
[content writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];
mailComposer = [[MFMailComposeViewController alloc]init];
mailComposer.mailComposeDelegate = self;
[mailComposer setSubject:@"Test mail"];
NSData *myData = [NSData dataWithContentsOfFile:fileName];
[mailComposer addAttachmentData:myData mimeType:@"text/plain" fileName:fileName];
[mailComposer setMessageBody:@"Testing message for the test mail" isHTML:NO];
[self presentModalViewController:mailComposer animated:YES];
這很好,我得到郵件附件。但是,如果我將文件名更改爲
NSString *fileName = [NSString stringWithFormat:@"%@",
@"Sample.txt"];
它顯示撰寫郵件中的附件,但發送時不會有任何附件。任何一個可以請建議如何來完成相同的