如何在Xcode5中附加到电子邮件PDF文件
我正在为Xcode5中的Ipad(iOS7.1)开发一个应用程序,该应用程序通过使用MFMailComposeViewController打开电子邮件模式视图控制器来发送电子邮件
我想附上PDF档案到我的电子邮件这是我的代码:
Functions.m:
#import <MessageUI/MessageUI.h>
@interface Functions()<MFMailComposeViewControllerDelegate>
@end
@implementation Functions
-(void)sendEmailInViewController:(UIViewController *)viewController {
NSString *emailTitle = @"Hello";
NSArray *toRecipents = [[NSArray alloc]initWithObjects:@"jesus@mymail.com", nil];
NSMutableString *messageBody =[[NSMutableString alloc]init];
[messageBody appendString:@"<p> </p><p> </p><p> </p><p><span style="font-size:14px;"><span style="color: rgb(0, 0, 102);"><span style="font-family: arial,helvetica,sans-serif;"><strong>Jesus</strong><br />Gerente Select / Sucursal Santa Fe<br />Paseo de la Lidia No. 801 Piso 8 Mod. 162<br />Col. Lomas del Pedregal, C.P. 01292, México D.F.<br />Tel: + (55) 8728-0908. Ext. 12832<br />e-mail: jesus@mymail.com</span></span></span></p><p><span style="color:#000066;"><span style="font-family: arial,helvetica,sans-serif;"></span></span></p>"];
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil) {
MFMailComposeViewController * mailView = [[MFMailComposeViewController alloc] init];
mailView.mailComposeDelegate = self;
//Set the subject
[mailView setSubject:emailTitle];
//Set the mail body
[mailView setMessageBody:messageBody isHTML:YES];
[mailView setToRecipients:toRecipents];
NSData *pdfData = [NSData dataWithContentsOfFile:@"google.pdf"];
[mailView addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"google.pdf"];
//Display Email Composer
if([mailClass canSendMail]) {
[viewController presentViewController:mailView animated:YES completion:NULL];
}
}
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
switch (result){
case MFMailComposeResultCancelled:NSLog(@"Mail cancelled"); break;
case MFMailComposeResultSaved: NSLog(@"Mail saved"); break;
case MFMailComposeResultSent: NSLog(@"Mail sent"); break;
case MFMailComposeResultFailed: NSLog(@"Mail sent failure: %@", [error localizedDescription]); break;
default: break;
}
// Close the Mail Interface
if (!app) { app = (AppDelegate *)[[UIApplication sharedApplication] delegate]; }
if (!currentSplitViewController) {
currentSplitViewController = (UISplitViewController *) app.window.rootViewController;
}
navController = [currentSplitViewController.viewControllers lastObject];
[[navController topViewController] dismissViewControllerAnimated:YES completion:NULL];
}
@end
在我用这种方法调用我的方法的任何viewController上:
- (IBAction)showEmail:(id)sender {
[functions sendEmailInViewController:self];
}
这是我的邮件的屏幕截图:
它显示我的PDF文件的标志,但是当我的信息被发送时,我打开了我的收件箱,但我只是找到签名,但附加任何查看...如何正确附加和发送?
任何帮助,我会感激
编辑:这是答案:我只需要添加这个
NSData *pdfData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"google" ofType:@"pdf"]];
[mailView addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"google"];
这是我的结果:
听起来像你需要看看MFMailComposeViewController的“ addAttachmentData:mimeType:fileName:
”方法。
并且使用这种方法,您可以添加原始NSData用于您想要发送的任何存档。 只要确定你设置了合适的MIME类型(例如“ application/zip
”)和文件名(例如“MyArchive.zip”)即可。
对于PDF文件,MIME类型应该是“ application/pdf
”,您可以使用像“google.pdf”这样的文件名。 然后你需要做的就是加载一个NSData对象和你想要附加的文件数据。
上一篇: how to attach to an email a PDF file in Xcode5
下一篇: python and matplotlib server side to generate .pdf documents