ios - 如何在iOS中将PDF文件附加到邮件和发送邮件

标签 ios iphone

目前,我正在开发费用管理应用程序,我想将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/

相关文章:

ios - 横向模态模式在关闭时切换为纵向模式

ios - 应用内购买和升级 iOS 应用

ios - 服务器的引号 (") appears as "â"在 iOS 应用程序中

iOS - AVPlayer seekToTime 重新开始播放 currentItem

iphone - XCode 4 在尝试运行项目时显示 "Argument Invalid"

iphone - cocoa 、 cocoa 触感和垂直分离器

php - 如何使用 XML 将对象从 iPhone 应用程序发送到服务器应用程序?

objective-c - 查找文件大小

ios - NSString 中的 Unicode 符号尺寸错误

iphone - 在 iOS 中检测拖动时的碰撞