0

我想在我的应用程序中发送一封带有 .PHP 文件作为附件的邮件:

if ([MFMailComposeViewController canSendMail])
{
    MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
    mailViewController.mailComposeDelegate = self;
    [mailViewController setSubject:@""];
    [mailViewController setMessageBody:@"" isHTML:NO];
    mailViewController.navigationBar.tintColor = [UIColor blackColor];
    NSData *myData = [NSData dataWithContentsOfFile:[self getScriptPath]];
    NSString *fileName = @"upload.php";
    [mailViewController addAttachmentData:myData mimeType:@"text/html" fileName:fileName];
    [self presentViewController:mailViewController animated:YES completion:nil];
    [mailViewController release];
}
else 
    NSLog(@"Device is unable to send email in its current state.");

正在发送邮件,但没有附件!...

4

3 回答 3

3

我敢打赌,您的[self getScriptPath]方法返回无效路径或不存在文件的路径,因此[NSData dataWithContentsOfFile:...]可能会返回nil.

您应该使用断点或 NSLog 来检查返回的值getScriptPath并相应地修复它。

例如,如果您将"upload.php"文件嵌入到您的应用程序中(您将它添加到您的 Xcode 项目中,以及您的应用程序的其他资源中),您应该使用它来获取脚本文件在您的包中的有效路径:

-(NSString*)getScriptPath
{
    return [[NSBundle mainBundle] pathForResource:@"upload" ofType:@"php"];
}

有关更多信息,请参阅Bundle Programming Guide文档。

于 2012-11-07T13:08:56.940 回答
0

使用此代码附加图像文件。

UIImage *emailImage = [UIImage imageNamed:@"loveCoding.png"];
[mailViewController addAttachmentData:UIImageJPEGRepresentation(emailImage, 1) mimeType:@"image/png" fileName:@"loveCocoa.png"];

附加文件

    NSString *fileName = [NSString stringWithFormat:@"Documents/%@.txt",sGame.userName];
    NSString *fullFilePath = [NSHomeDirectory() stringByAppendingPathComponent:fileName];

    NSString *emailFileName = [NSString stringWithFormat:@"%@_Report.txt",sGame.userName];

    NSData *myData = [NSData dataWithContentsOfFile:fullFilePath];

    [email addAttachmentData:myData mimeType:@"text/plain" fileName:emailFileName];
于 2012-11-07T13:05:49.003 回答
0

看起来您的 NSData 没有正确加载。从 getScriptPath 检查您的路径

于 2012-11-07T13:07:02.717 回答