У меня есть головной скребок, я создал UITableView для отображения записанных данных, хранящихся в каталоге документов, что позволяет мне выбирать несколько файлов и отправлять их по электронной почте. Это отлично работает, я выбираю перечисленные файлы, которые хочу отправить в свой адрес электронной почты, нажмите кнопку электронной почты и появится электронное письмо со списком вложенных файлов. Имена и расширения верны.MFMailComposeViewController потеряет вложения после отправки
Проблема в том, что я отправил электронное письмо, и после его получения вложения исчезли и были заменены файлами txt с такими именами, как ATT00001.txt, ATT00002.txt и т. Д.
Может кто-нибудь объяснить мне, почему это происходит и как это можно исправить? Я перечислил свой код ниже:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [filePathsArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.csv'"];
NSArray *csvFiles = [fileList filteredArrayUsingPredicate:fltr];
NSLog(@"Contents of directory: %@", csvFiles);
filePathsArray = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:documentsDirectory error:nil];
cell.textLabel.text = [csvFiles objectAtIndex:indexPath.row];
return cell;
}
# pragma mark - Deleting data from Row.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
{
NSString *fileName = [filePathsArray objectAtIndex:indexPath.row];
NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [paths objectAtIndex:0];
path = [path stringByAppendingPathComponent:fileName];
NSError *error;
[filePathsArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView reloadData];
if ([[NSFileManager defaultManager]fileExistsAtPath:path]) {
if(![[NSFileManager defaultManager] removeItemAtPath:path error:&error])
{
NSLog(@"Delete file error:%@", error);
}
NSLog(@"Deleting file named: %@", path);
}
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.selectedData addObject:[filePathsArray objectAtIndex:indexPath.row]];
[[tableView indexPathsForSelectedRows] count];
[self updateEmailButtonTitle];
NSLog(@"selectedData %@",self.selectedData);
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
[self.selectedData removeObject:[filePathsArray objectAtIndex:indexPath.row]];
[self updateEmailButtonTitle];
NSLog(@"deselectedData %@",self.selectedData);
}
}
#pragma mark - Email Selected Data
-(IBAction)emailButton:(id)sender
{
[self showEmail];
}
- (void)showEmail {
NSString *emailTitle = @"Your Data";
NSString *messageBody = @"Attached is your recorded data.";
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
for (NSString *file in self.selectedData) {
// Determine the file name
NSString *filename = [self.selectedData objectAtIndex:0];
// Read the file using NSData
NSData *fileData = [NSData dataWithContentsOfFile:file];
// Add attachment
[mc addAttachmentData:fileData mimeType:@"text/csv" fileName:filename];
}
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail sent failure: %@", [error localizedDescription]);
break;
default:
break;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}
Любая помощь будет принята с благодарностью.
EDIT - вот готовый и рабочий код, спасибо всем, кто помог с этим:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [filePathsArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.csv'"];
NSArray *csvFiles = [fileList filteredArrayUsingPredicate:fltr];
NSLog(@"Contents of directory: %@", csvFiles);
filePathsArray = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:documentsDirectory error:nil];
cell.textLabel.text = [csvFiles objectAtIndex:indexPath.row];
return cell;
}
# pragma mark - Deleting data from Row.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
{
NSString *fileName = [filePathsArray objectAtIndex:indexPath.row];
NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [paths objectAtIndex:0];
path = [path stringByAppendingPathComponent:fileName];
NSError *error;
[filePathsArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView reloadData];
if ([[NSFileManager defaultManager]fileExistsAtPath:path]) {
if(![[NSFileManager defaultManager] removeItemAtPath:path error:&error])
{
NSLog(@"Delete file error:%@", error);
}
NSLog(@"Deleting file named: %@", path);
}
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.selectedData addObject:[filePathsArray objectAtIndex:indexPath.row]];
[[tableView indexPathsForSelectedRows] count];
[self updateEmailButtonTitle];
NSLog(@"selectedData %@",self.selectedData);
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
[self.selectedData removeObject:[filePathsArray objectAtIndex:indexPath.row]];
[self updateEmailButtonTitle];
NSLog(@"deselectedData %@",self.selectedData);
}
}
-(void)updateEmailButtonTitle
{
NSArray *selectedRows = [self.tableView indexPathsForSelectedRows];
if (selectedRows.count == self.selectedData.count)
{
self.emailButton.enabled = NO;
self.emailButton.title = @"Email";
} else if (selectedRows.count == 0) {
self.emailButton.enabled = YES;
self.emailButton.title = @"Email Selected Data";
}
}
#pragma mark - Email Selected Data
-(IBAction)emailButton:(id)sender
{
[self showEmail];
}
- (void)showEmail
{
NSString *emailTitle = @"Your Data";
NSString *messageBody = @"Attached is your recorded data.";
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
for (NSString *file in self.selectedData) {
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *csvFilePath = [documentsDirectory stringByAppendingPathComponent:file]; // This will check the conents of the string "file" and match it with files located in the documents directory.
NSData *myData = [NSData dataWithContentsOfFile:csvFilePath];
NSLog(@"my nsdata is %@",myData); //check whether your nsdata is nil or not
[mc addAttachmentData:myData
mimeType:@"text/csv"
fileName:file];
}
[self presentViewController:mc animated:YES completion:nil];
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail sent failure: %@", [error localizedDescription]);
break;
default:
break;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}
ли ATT00001.txt содержат какие-либо данные? Данные в документе txt верны, как вы сохранили? – Vidhyanand
@ Видхиан и, к сожалению, нет, это пустые файлы. –
простой. Файлы csv не прикрепляются к вашей электронной почте. вы можете увидеть значок файла csv, прикрепленный к электронной почте, но на самом деле к нему не прикреплен файл csv. –