我在xcode中编写了一个小脚本,它在我的应用程序中包含一个CSV文件并将其作为附件发送。当我点击电子邮件按钮时,一切正常(即主题,身体等)。它也显示了CSV文件但是当我检查我的电子邮件时,我看到除了csv文件之外的所有内容。我很难过。这是我使用的代码。我也试过了文件名:@“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];
答案 0 :(得分:0)
我现在面临着和你一样的问题。在寻找解决方案时,我发现了iOS和Mac OS的一个特点。
您无法发送文件的原因非常简单,这是因为您不允许在应用程序包或iOS文档文件夹中创建文件。
例如,如果您创建一个文件并将其插入XCODE上的app文件夹,而不是将其用作mailComposeViewController上的附件,您将收到一封包含该文件的邮件。这里的压力部分是你甚至无法更新应用程序内文件的内容。
如果您使用Xcode和许多NSLOG进行测试来跟踪您的代码和数据流,您将看到您的代码可能在Mac OS上运行,但如果您在设备上运行模拟,您会发现一个完全不同的场景。
iOS的SANDBOX禁止这种操作,主要是为了保护用户信息。 但是有希望!
共享应用程序内部生成的数据的一种可能解决方案是将此信息作为NSString传递给MailComposerViewController,这样您就无法发送所需的数据。
是的,是的...我知道,这不是优雅,但这是我们到目前为止所得到的。如果你发现了一种没有jailbrake的方法,你的设备可以与我们分享。
答案 1 :(得分:0)
将此添加到您的viewcontroller.m
然后添加:
- (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];
}
}