将html文本作为pdf格式作为ios中的附件发送到邮件

时间:2013-07-05 14:05:09

标签: iphone ios objective-c mfmailcomposeviewcontroller

我必须将html文本作为带有pdf扩展名的附件发送到邮件。

代码:

    MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
    mailViewController.mailComposeDelegate = self;
    NSArray *toReceipents =[NSArray arrayWithObjects:@"", nil];
    [mailViewController setToRecipients:toReceipents];
    [mailViewController setSubject:strMailMessage];
    NSLog(@"Mail Message:%@ %@",strMailMessage,appDelegate.strShareText);
    NSData* data = [appDelegate.strShareText dataUsingEncoding:NSUTF8StringEncoding];
    [mailViewController setMessageBody:appDelegate.strShareText isHTML:YES];
    [mailViewController addAttachmentData:data mimeType:@"application/pdf" fileName:@"Medication file.pdf"];
    [self presentModalViewController:mailViewController animated:YES];
    [mailViewController release];

注意:当我下载pdf文件时,我得到相同的文本。但我想在pdf文档中以表格格式显示文本

1 个答案:

答案 0 :(得分:0)

好的代码但是对于HTML尝试这个;):

- (IBAction)mailCompose:(id)sender {

        MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
        mail.mailComposeDelegate = self;

        [mail setSubject:@"Hello World!"];
        NSArray *toRecipients = [NSArray arrayWithObjects:@"e-mail here or leve empty", nil];

        [mail setToRecipients:toRecipients];
        NSString *emailBody = @"Body message App</b><br /><a href='https://itunes.apple.com/it/app/yourApp/id'>Download Free on AppStore!</a>";

        NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
        NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"myVoice.caf"];
        NSData *myData = [NSData dataWithContentsOfFile:filePath];
        [mail addAttachmentData:myData mimeType:documentsDirectory fileName:@"myVoice.caf"];


        [mail setMessageBody:emailBody isHTML:YES];
        mail.modalPresentationStyle = UIModalPresentationPageSheet;
        [self presentViewController:mail animated:YES completion:nil];

}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {

    switch (result) {
        case MFMailComposeResultCancelled:
            NSLog(@"Cancelled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Saved");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Faild");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Sent");
            break;
        default:
            NSLog(@"Default");
            break;
    }

    [self dismissViewControllerAnimated:YES completion:nil];

}

Enjy希望这可以帮到你