0

我在 xcode 中编写了一个小脚本,它在我的应用程序中获取一个 CSV 文件并将其作为附件发送。当我单击电子邮件按钮时,一切正常(即主题、正文等)。它也显示 CSV 文件,但是当我检查我的电子邮件时,我看到了除 csv 文件之外的所有内容。我难住了。这是我使用的代码。我也试过 filename:@"isitlist.csv。csv 文件位于我的主文件夹中。

if ([MFMailComposeViewController canSendMail])
{
    MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];

    mailer.mailComposeDelegate = self;

    [mailer setSubject:@"Guest List Form"];

    NSArray *toRecipients = [NSArray arrayWithObjects:@"", nil];
    [mailer setToRecipients:toRecipients];

    [mailer addAttachmentData:[NSData dataWithContentsOfFile:@"isit_list.csv"]
                     mimeType:@"text/csv"
                     fileName:@"isit_list"];

    [self presentModalViewController:mailer animated:YES];

    NSString *emailBody = @"Please fill out the attached file and then email it back to us. Also, please attach some photos to use for the slideshow. We suggest no more than 10.";
    [mailer setMessageBody:emailBody isHTML:NO];

    // only for iPad
    // mailer.modalPresentationStyle = UIModalPresentationPageSheet;

    [self presentModalViewController:mailer animated:YES];

    [mailer release];
4

2 回答 2

0

I`m facing the same problem as you right now. And while looking for a solution I discovered one peculiarity of iOS and Mac OS.

the reason why you can't send a file is very simple, it's because you are not allowed to create a file in to your app bundle or your iOS documents folder.

For exemple if you create a file and insert it on your app folder on XCODE, than use that as a attachment on mailComposeViewController, you will receive a mail with the file as expected. the stressful part here is that you can not even update the content of the file inside your app.

If you make a test using Xcode, and LOTS of NSLOGs to track your code, and the data flow, you will see that your code will probable work on Mac OS, but if you run the simulation on a device you will find a completely diferente scenario.

The SANDBOX of iOS forbids this kind of operation, mostly to protect user information. But there is hope!

One possible solution to share the data generated inside app is to pass this information as a NSString to MailComposerViewController, this way you cant send the data you want.

yeah yeah... i know, this is not elegant, but this is what we got so far.

if you discovered a way to do this without jailbrake your device share that with us.

于 2013-03-14T20:31:44.147 回答
0

将此添加到您的 viewcontroller.m

导入 MessageUI/MFMailComposeViewController.h

导入 MessageUI/MessageUI.h

然后添加这个:

-(IBAction)发送:(id)发件人;{

if ([MFMailComposeViewController canSendMail])
{
    
    NSString *textFileContentsString = @"Last Name (i.e. Smith), First Name (i.e. Mr and Mrs. James), Table Number (i.e. 7)\nJones,Mr and Mrs Steve, 6\nJames,Mr and Mrs Albert,8\nJohnson, Mr and Mrs Fred, 7";
    NSData *textFileContentsData = [textFileContentsString dataUsingEncoding:NSASCIIStringEncoding];
    MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
    mailComposeViewController.mailComposeDelegate = self;
     NSArray *toRecipients = [NSArray arrayWithObject:@""];
    [mailComposeViewController setSubject:@"My Sample Project"];
    [mailComposeViewController setMessageBody:@"Send this email to the email address that you use on the iPad. When you open it in your email you will first save the photos to your camera roll by pressing and holding on the image. Then you will press on the isit_list.csv file and click on 'Open in iSIT'. You will then see a message that the data has been imported. You will then create your event by pressing on 'Enter Seating Admin'. Enter in the Couple's Names, Event Date, Select Photos (these are photos for the slideshow), then click 'Save Event'. You will notice the event is saved. To start the event click on 'Load Event', select a Theme to use, then click 'Launch Event'. The sample data in the CSV file only contains a few names which all have a last name that start with'J'. So click on 'J' when you see the alphabet screen. " isHTML:NO];
    [mailComposeViewController addAttachmentData:textFileContentsData mimeType:@"text/plain" fileName:@"isit_list.csv"];

    [mailComposeViewController setToRecipients:toRecipients];
    [self presentModalViewController:mailComposeViewController animated:YES];
}
else
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
                                                    message:@"Your device doesn't support the composer sheet"
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];

}

}

于 2013-03-21T02:02:00.137 回答