2012-04-25 27 views
3

我已经从在线生成pdf文件。看到PDF时,我想通过邮件发送该pdf,并自动附上该pdf。我使用了很多代码,但一切正常pdf.can任何人都可以帮助我。用我的邮件附上PDF/Doc文件

+2

什么是你的问题?你有什么尝试?你遇到过什么错误? – sosborn 2012-04-25 05:02:57

+0

“我使用了很多代码,但是对于单个pdf,一切正常。任何人都可以帮助我。”如果它工作正常,你需要什么帮助? – iNoob 2012-04-25 05:04:07

+0

@sosborn。我没有错。在iPhone看到照片的同时,您可以通过邮件发送照片,照片将自动附加。就像我想发送PDF文件 – 2012-04-25 05:08:14

回答

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"); 
} 
+0

现在的问题是我的文件名将不得不改变,这取决于我所看到的pdf。但它的名字都是相同的。 – 2012-04-25 05:27:05

+1

然后动态更改名称。 Gypsa只是给你示例代码 - 这取决于你将它整合在一起。 – sosborn 2012-04-25 06:14:00

0

MIME类型的PDF变化,从而使用此MIME类型是worksfine我

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"]; 
+0

我正在做类似的事情,但当我按下“发送”发送电子邮件时,应用程序卡住了。它只是不发送..任何人有这个问题? – 2013-07-25 18:57:00

1

感谢@Gypsa
这里是SWIFT代码

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 
    }) 
}