3

我已经从网上生成了 pdf 文件。在看到 pdf 时,我想通过邮件发送该 pdf,并自动附加该 pdf。我使用了很多代码,但对于单个 pdf 来说一切正常。任何人都可以帮助我。

4

3 回答 3

9

尝试这个,

if([MFMailComposeViewController canSendMail]){      

    MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
    mail.mailComposeDelegate=self;
    [mail setSubject:@"Email with attached pdf"];   
    NSString *newFilePath = @"get path where the pdf reside";

    NSData * pdfData = [NSData dataWithContentsOfFile:newFilePath];
[mail addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"yourpdfname.pdf"];
    NSString * body = @"";
    [mail setMessageBody:body isHTML:NO];
    [self presentModalViewController:mail animated:YES];
    [mail release];         
}
else
{
    //NSLog(@"Message cannot be sent");
}
于 2012-04-25T05:03:55.670 回答
1

谢谢@Gypsa
这是快速代码

func composeMail(){        
        if(MFMailComposeViewController.canSendMail()){

            var mail:MFMailComposeViewController = MFMailComposeViewController()
            mail.mailComposeDelegate = self

            mail.setSubject("Email with attached pdf")

            //file name "attatchment.pdf" in project bundle
            var newFilePath:NSString = NSBundle.mainBundle().pathForResource("attatchment", ofType: "pdf")!

            var pdfData:NSData = NSData(contentsOfFile: newFilePath as String)!
            mail.addAttachmentData(pdfData, mimeType: "application/pdf", fileName: "attatchment.pdf")

            var body:NSString = ""
            mail.setMessageBody(body as String, isHTML: false)
            self.presentViewController(mail, animated: true) { () -> Void in

            }

        }else{

            println("Message cannot be sent")
        }
    }

    // MARK: - MFMailComposeViewControllerDelegate
    func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!)
    {
        self.dismissViewControllerAnimated(true, completion: { () -> Void in
        })
    }
于 2015-09-15T07:06:20.513 回答
0

mime 类型是 pdf 的更改,所以使用这种 mime 类型它对我来说很好用

NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, bounds, nil);

然后在将来的某个时候,您需要将该 pdfData 传递给 MFMailComposeViewController。

MFMailComposeViewController *vc = [[[MFMailComposeViewController alloc] init] autorelease];
[vc setSubject:@"my pdf"];
[vc addAttachmentData:pdfData mimeType:@"image/pdf" fileName:@"SomeFile.pdf"];
于 2012-10-17T14:10:56.397 回答