문서 디렉토리 iPad에서 텍스트 파일 가져 오기
문제
모두 내 요구 사항에서 문서 디렉토리에있는 첨부 된 텍스트 파일로 메일을 보내야합니다. 여기서는 문서 디렉토리에있는 파일의 경로를 얻을 수 있습니다. iPad에서 파일을 어떻게 첨부 할 수 있습니까?
여기에 내가 시도한 코드가
MFMailComposeViewController *picker=[[MFMailComposeViewController alloc]init];
picker.mailComposeDelegate=self;
if ([MFMailComposeViewController canSendMail]){
// Create and show composer
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSLog(@"directry path %@",paths);
NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:@"Logs/log-858743.txt"];
NSLog(@"directry full path %@",fullPath);
NSData *data = [NSData dataWithContentsOfFile: fullPath];
NSLog(@"Data value %@",data);
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath];
if (fileExists) {
NSLog(@"file is there");
}else{
NSLog(@"file is not there");
}
[picker setSubject:@"Backup"];
[picker addAttachmentData:data mimeType:@"text/rtf" fileName:@"log.txt"];
[picker setMessageBody:@"testing." isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release]; }
else{
// Show some error message here
}
. 해결책
Here a sample, try this
MFMailComposeViewController *mViewController = [[MFMailComposeViewController alloc] init];
mViewController.mailComposeDelegate = self;
//Imposto l'oggetto
[mViewController setSubject:@"Invio Copia Commissione"];
//Preparo l' A: Devo spezzare gli indirizzi E-mail
NSArray *toRecipients = [NSArray arrayWithArray:[Globals.AEmail componentsSeparatedByString:@";"]];
[mViewController setToRecipients:toRecipients];
//Preparo il Cc: Devo spezzare gli indirizzi E-mail
if ([Config.EmailAgg length] != 0) {
NSArray *CcRecipients = [NSArray arrayWithArray:[Config.EmailAgg componentsSeparatedByString:@";"]];
[mViewController setCcRecipients:CcRecipients];
}
//Setto il corpo dell'E-mail
[mViewController setMessageBody:Globals.TestoEmail isHTML:NO];
//A questo punto visto che ho fatto 30 salvo il testo anche come file allegato dell'E-mail
[Globals.TestoEmail writeToFile:[NSString stringWithFormat:@"%@/CopiaCommissione.txt", Globals.DocumentsPath] atomically:TRUE encoding:NSUTF8StringEncoding error:nil];
NSData *Allegato = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/CopiaCommissione.txt", Globals.DocumentsPath]];
[mViewController addAttachmentData:Allegato mimeType:@"text/plain" fileName:@"CopiaCommissione.txt"];
[self presentModalViewController:mViewController animated:YES];
[mViewController release];
try to change mimeType in your code like this
[picker addAttachmentData:data mimeType:@"text/plain" fileName:@"log.txt"];
제휴하지 않습니다 StackOverflow