CSV not attaching to in app email?

2020-05-02 02:30发布

问题:

So I have had this problem for sometime and just cant get it working! I have been building a survey app that users simply enter information in and its saved to a csv file. Im now at the stage where I need to attached the csv file within the app to an email address...

I just tested this on my new i-phone and there is no attachment when the email is received? Its there in the mail app and in the simulator, however when the message is received on the email account the attachment has gone? Can anyone help?? My code is below:

- (IBAction)send:(id)sender {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *savedFilePath = [documentsDirectory stringByAppendingPathComponent:@"result‌s.csv"];
    NSData *csvData = [NSData dataWithContentsOfFile:savedFilePath];

    MFMailComposeViewController *mailcomposer = [[MFMailComposeViewController alloc] init];
    [mailcomposer addAttachmentData:csvData mimeType:@"text/csv" fileName:@"results.csv"];
    [mailcomposer setToRecipients:@[@"gpsflighttrial@gcap.eu"]];
    [mailcomposer setSubject:self.subject.text];
    [mailcomposer setMessageBody:self.message.text isHTML:NO];
}

回答1:

Change your mimeType to @"application/csv" and it will work.



回答2:

Reason for csv not being attached can be ether of below :

1) filename or filepath is wrong as result nsdata of csv will be nil.

2) filename in addAttachmentData mimeType: fileName: method should cantain only name not type of file format.

Changes in your method is given below :

-(IBAction)send:(id)sender {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    // Determine the file name and extension
    NSString *strFileName = @"results.csv";
    NSArray *filepart = [strFileName componentsSeparatedByString:@"."];
    NSString *filename = [filepart objectAtIndex:0];

    //get file path
    NSString *savedFilePath = [documentsDirectory stringByAppendingPathComponent:strFileName];

    //Now check if file exits
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:savedFilePath]){ 
       NSData *csvData = [NSData dataWithContentsOfFile:savedFilePath];

       if(csvData)
       {
          MFMailComposeViewController *mailcomposer = [[MFMailComposeViewController alloc] init];
          [mailcomposer addAttachmentData:csvData mimeType:@"text/csv" fileName:filename];
          [mailcomposer setToRecipients:@[@"gpsflighttrial@gcap.eu"]];
          [mailcomposer setSubject:self.subject.text];
          [mailcomposer setMessageBody:self.message.text isHTML:NO];
       }
       else
          NSLog(@"error csv data not created");
    }
    else
       NSLog(@"error file doesnot exists");

}


回答3:

So after many sleepless nights and a lot of trying different ways I finally found the way that works with a friend (see code below)

 NSString *docsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSData *attachment = [NSData dataWithContentsOfFile:[docsDirectory stringByAppendingPathComponent:@"results.csv"]];

    MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
    mailer.mailComposeDelegate = self;
    [mailer setToRecipients:@[@"gpsflighttrial@gcap.eu"]];
    [mailer setSubject:self.subject.text];
    [mailer setMessageBody:self.message.text isHTML:NO];
    [mailer addAttachmentData:attachment mimeType:@"application/csv" fileName:@"results.csv"];
    [self presentModalViewController:mailer animated:YES];

Just in case anyone has this problem in the future.