目前,我正在开发费用管理应用程序,我想将PDF文件附加到邮件中并将其发送到指定的电子邮件地址。因此,从我应该从哪里开始以及如何开发它的角度指导我。
谢谢。
MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init];
vc.mailComposeDelegate = self;
[vc setSubject:@"Monthly Expense Report"];
[vc addAttachmentData:pdfData mimeType:@"application/pdf" fileName:[NSString stringWithFormat:@"%@.pdf",[arrOfExpenseDate objectAtIndex:1]]];
if ([MFMailComposeViewController canSendMail])
{
[self presentViewController:vc animated:YES completion:nil];
}
最佳答案
我为您创建了一个有关如何使用MFMailComposeViewController发送pdf附件的示例。以下是代码:
@interface ViewController () <MFMailComposeViewControllerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)compseEmail:(id)sender
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init];
vc.mailComposeDelegate = self;
[vc setSubject:@"Monthly Expense Report"];
NSString *pdfPath = [[NSBundle mainBundle] pathForResource:@"ApacheInstallationSteps" ofType:@"pdf"];
NSData *pdfData = [NSData dataWithContentsOfFile:pdfPath];
[vc addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"ApacheInstallationSteps.pdf"];
[self presentViewController:vc animated:YES completion:nil];
}
else
{
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Alert!!!" message:@"Your device can't send emails." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Result: canceled");
break;
case MFMailComposeResultSaved:
NSLog(@"Result: saved");
break;
case MFMailComposeResultSent:
NSLog(@"Result: sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Result: failed");
break;
default:
NSLog(@"Result: not sent");
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
确保添加MessageUI.framework。以下是输出。
关于ios - 如何在iOS中将PDF文件附加到邮件和发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24325549/