如何从头开始创建带有pdf附件的电子邮件

时间:2014-01-08 01:15:14

标签: ios objective-c pdf email-attachments

嗨,我完全无能为力,当用户点击按钮发送“电子邮件”时,我想在电子邮件上发送pdf附件。我试着这样做@implementation AttachmentTableViewController

- (id)initWithStyle:(UITableViewStyle)style
 {
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

_files = @[@"Elementry application 2015.pdf",@"HS application 2015.pdf"];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [_files count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath                  *)indexPath
{
 static NSString *CellIdentifier = @"Cell";
 AttachmentTableViewCell *cell = (AttachmentTableViewCell*)[tableView     dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// Configure the cell...
cell.fileLabel.text = [_files objectAtIndex:indexPath.row];
cell.thumbnail.image = [UIImage imageNamed:[NSString stringWithFormat:@"icon%d.png",    indexPath.row]];

return cell;
}


 #pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *selectedFile = [_files objectAtIndex:indexPath.row];
[self showEmail:selectedFile];
}


- (void)showEmail:(NSString*)file {

NSString *emailTitle = @"Registration Forms Ateres App";
NSString *messageBody = @"Send forms to your email so you can print it out and fill out the       form!!";
NSArray *toRecipents = [NSArray arrayWithObject:@""];

MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
 [mc setToRecipients:toRecipents];

// Determine the file name and extension
NSArray *filepart = [file componentsSeparatedByString:@"."];
NSString *filename = [filepart objectAtIndex:0];
NSString *extension = [filepart objectAtIndex:1];

// Get the resource path and read the file using NSData
NSString *filePath = [[NSBundle mainBundle] pathForResource:filename ofType:extension];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];

// Determine the MIME type
NSString *mimeType;
if ([extension isEqualToString:@"jpg"]) {
    mimeType = @"image/jpeg";
} else if ([extension isEqualToString:@"png"]) {
    mimeType = @"image/png";
} else if ([extension isEqualToString:@"doc"]) {
    mimeType = @"application/msword";
} else if ([extension isEqualToString:@"ppt"]) {
    mimeType = @"application/vnd.ms-powerpoint";
} else if ([extension isEqualToString:@"html"]) {
    mimeType = @"text/html";
} else if ([extension isEqualToString:@"pdf"]) {
    mimeType = @"application/pdf";
}

// Add attachment
[mc addAttachmentData:fileData mimeType:mimeType fileName:filename];

// Present mail view controller on screen
[self presentViewController:mc 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
[self dismissViewControllerAnimated:YES completion:NULL];
}

@end
 //but it wants a AttachmentTableViewCell.h and .m but for some reason i don't know how i can   attach that with main story board, like attach the label and make an ib outlet on the cell like this   @interface AttachmentTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *thumbnail;
@property (weak, nonatomic) IBOutlet UILabel *fileLabel;

  @end
有人请帮帮我 我希望它是一个表格形式,当你点击它时,它会直接发送电子邮件,其中已经附加了pdf,这是代码可以从中提取但我想从我正在创建的应用程序中添加,所以如果有谁知道该怎么做请回答!!  http://www.appcoda.com/ios-programming-create-email-attachment/

1 个答案:

答案 0 :(得分:0)

我们创建一个按钮并为其附加一个动作。我们在动作方法中写下以下内容:

- (IBAction)generatePdfButtonPressed:(id)sender
{
    pageSize = CGSizeMake(612, 792);
    NSString *fileName = @"Demo.pdf";
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];

   [self generatePdfWithFilePath:pdfFileName];

}

更多参考Click

希望它对你有用.. :))