我正在尝试创建一个CSV文件,该文件是来自iPad的电子邮件(CSV文件也是在iPad上创建的)。
然而,我的代码不起作用,我认为它正在创建CSV文件并找到要发送的文件,但当我单击发送并检索电子邮件时,文件不是通过电子邮件发送的,但电子邮件上没有文件。
有人能帮我吗?
我愿意回答任何问题。
谢谢Alex
- (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];
}