我的 CSV 创建代码有什么问题

What wrong with my CSV creation code

我正在尝试创建一个 CSV 文件,该文件是来自 iPad 的电子邮件(CSV 文件也在 iPad 上创建)。

但是我的代码不起作用我认为它正在创建 CSV 文件并找到要发送的文件但是当我单击发送并检索电子邮件时文件没有通过电子邮件发送但是电子邮件上没有文件.

有什么可以帮助我的吗?

我愿意回答任何问题。

谢谢 亚历克斯

- (IBAction)save:(id)sender {
    NSString *resultline=[NSString stringWithFormat:@"%@,%@,%@,%@,%@,%@,%@.%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,togS4.5%@,togS7.0%@,togS9.0%@,togS10.5%@,togS13.5%@,togS15.0%@,togD4.5%@,togD7.0%@,togD9.0%@,togD10.5%@,togD13.5%@,togD15.0%@,togK4.5%@,togK7.0%@,togK9.0%@,togK10.5%@,togK13.5%@,togK15.0%@,togSK4.5%@,togSK7.0%@,togSK9.0%@,togSK10.5%@,togSK13.5%@,togSK15.0%@,togE4.5%@,togE7.0%@,togE9.0%@,togE10.5%@,togE13.5%@,togE15.0%@,togA4.5%@,togA7.0%@,togA9.0%@,togA10.5%@,togA13.5%@,togA15.0%@,MIS%@\n",
                          self.custNameLabel4.text,
                          self.dateLabel4.text,
                          self.farbicLabel4.text,
                          self.fibreLabel4.text,
                          self.boundLabel4.text,
                          self.quantityLabel4.text,
                          self.sizeLabel4.text,
                          self.sasshlabel3.text,
                          self.innaLabel3.text,
                          self.packedLabel3.text,
                          self.outerLabel3.text,
                          self.palletLabel3.text,
                          self.weeksLabel2.text,
                          self.devileryLabel2.text,
                          self.customerLabel2.text,
                          self.DateLabel1.text,
                          self.textviewtext.text,
                          self.requested.text,
                          self.Elastic.text,
                          self.t1.text,
                          self.t2.text,
                          self.t3.text,
                          self.t4.text,
                          self.t5.text,
                          self.t6.text,
                          self.t7.text,
                          self.t8.text,
                          self.t9.text,
                          self.t10.text,
                          self.t11.text,
                          self.t12.text,
                          self.t13.text,
                          self.t14.text,
                          self.t15.text,
                          self.t16.text,
                          self.t17.text,
                          self.t18.text,
                          self.t19.text,
                          self.t20.text,
                          self.t21.text,
                          self.t22.text,
                          self.t23.text,
                          self.t24.text,
                          self.t25.text,
                          self.t26.text,
                          self.t27.text,
                          self.t28.text,
                          self.t29.text,
                          self.t30.text,
                          self.A1.text,
                          self.A2.text,
                          self.A3.text,
                          self.A4.text,
                          self.A5.text,
                          self.MIS.text];
    NSString *docPath =[NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES)objectAtIndex:0];
    _textviewtext.text = resultline;
    NSString *topper=[docPath stringByAppendingPathComponent:@"/topper.csv"];

    [resultline writeToFile:topper atomically:YES encoding:NSUTF8StringEncoding error:nil];
    //NSLog(@"docpath saved");

    {MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate  = self;

        [picker setToRecipients:[NSArray arrayWithObjects:@"email Address", nil]];
        [picker setSubject:self.custNameLabel4.text];
        [picker setMessageBody:@"Message"isHTML:NO];
        [picker addAttachmentData:[NSData dataWithContentsOfFile:@"PathToFile.csv"]
                         mimeType:@"text/csv"
                         fileName:@"/topper.csv"];

        picker.navigationBar.barStyle = UIStatusBarStyleLightContent;

        [self presentViewController:picker animated:YES completion:NULL];
    }
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}

我猜你没有写入磁盘,也没有通过电子邮件发送文件的完整路径。

确保您已包含委托 MFMailComposeViewControllerDelegate 我已经测试了下面的代码,它工作正常

-(IBAction)save:(id)sender
{


    NSString *resultline=[NSString stringWithFormat:@"%@,%@,%@,%@,%@,%@,%@.%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,%@,togS4.5%@,togS7.0%@,togS9.0%@,togS10.5%@,togS13.5%@,togS15.0%@,togD4.5%@,togD7.0%@,togD9.0%@,togD10.5%@,togD13.5%@,togD15.0%@,togK4.5%@,togK7.0%@,togK9.0%@,togK10.5%@,togK13.5%@,togK15.0%@,togSK4.5%@,togSK7.0%@,togSK9.0%@,togSK10.5%@,togSK13.5%@,togSK15.0%@,togE4.5%@,togE7.0%@,togE9.0%@,togE10.5%@,togE13.5%@,togE15.0%@,togA4.5%@,togA7.0%@,togA9.0%@,togA10.5%@,togA13.5%@,togA15.0%@,MIS%@\n",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a",
                          @"a"];
    NSString *docPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];

    NSString *topper=[docPath stringByAppendingPathComponent:@"topper.csv"];

    [resultline writeToFile:topper atomically:YES encoding:NSUTF8StringEncoding error:nil];
    //NSLog(@"docpath saved");




    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate  = self;

        [picker setSubject:@"abc"];
        [picker setMessageBody:@"Message"isHTML:NO];
        [picker addAttachmentData:[NSData dataWithContentsOfFile:topper]
                         mimeType:@"text/csv"
                         fileName:@"topper.csv"];




    picker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    picker.modalPresentationStyle = UIModalPresentationFormSheet;


    [self presentViewController:picker animated:YES completion:NULL];



}



-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    // Notifies users about errors associated with the interface
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Result: canceled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Result: saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Result: sent");

            break;
        case MFMailComposeResultFailed:
            NSLog(@"Result: failed");
            break;
        default:
            NSLog(@"Result: not sent");
            break;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}