gpt4 book ai didi

ios - 从文档目录 Objective c 发送带有 pdf 附件的电子邮件

转载 作者:行者123 更新时间:2023-11-28 19:37:22 25 4
gpt4 key购买 nike

我在让我的程序接受我给它的路径并将其作为 NSData 返回时遇到问题

    // Get the resource path and read the file using NSData


NSString *searchFilename = @"SafetyAuditReport.pdf"; // name of the PDF you are searching for

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager] enumeratorAtPath:documentsDirectory];

NSString *documentsSubpath;
while (documentsSubpath = [direnum nextObject])
{
if (![documentsSubpath.lastPathComponent isEqual:searchFilename]) {
continue;
}

NSLog(@"found %@", documentsSubpath);
}

NSData *pdfData = [NSData dataWithContentsOfFile:documentsSubpath];


// Determine the MIME type
NSString *mimeType = @"application/pdf";




MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
[mc addAttachmentData:pdfData mimeType:mimeType fileName:@""];

NSLog(@"data was loaded.......");
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:nil];

当程序获取电子邮件部分时,它会为 addAttachmentData:pdfData mimeType:mimeType fileName: 抛出一个 nil 异常,表示我给它的变量是 nil (pdfData) 当我知道文件存在于文档目录中时。

提前致谢

最佳答案

请使用此代码

- (IBAction)sendMailWithAttachedFile
{

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
// NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:[self pathForResourse:fileName ofType:extension]];


//Get the file path
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.pdf"];

NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:path];
NSData *data=[[NSData alloc]initWithContentsOfURL:outputURL];
[picker addAttachmentData:data mimeType:@"application/pdf" fileName:@"data.pdf"];
[self presentModalViewController:picker animated:YES];


}

-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self dismissViewControllerAnimated:YES completion:nil];
}

关于ios - 从文档目录 Objective c 发送带有 pdf 附件的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37533504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com