我想在我的应用中使用.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.");
正在发送邮件,但没有附件!...
答案 0 :(得分:3)
我敢打赌,您的[self getScriptPath]
方法会返回无效路径或指向不存在文件的路径,因此[NSData dataWithContentsOfFile:...]
可能会返回nil
。
您应该使用断点或NSLog来检查getScriptPath
返回的值并相应地修复它。
例如,如果您在应用程序中嵌入了"upload.php"
文件(您在Xcode项目中添加了它,以及应用程序的其他资源),您应该使用它来获取脚本文件的有效路径在你的包中:
-(NSString*)getScriptPath
{
return [[NSBundle mainBundle] pathForResource:@"upload" ofType:@"php"];
}
有关详细信息,请参阅Bundle Programming Guide文档。
答案 1 :(得分: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];
答案 2 :(得分:0)
看起来您的NSData未正确加载。检查getScriptPath的路径