1

我必须将 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 文档中以表格格式显示文本

4

1 回答 1

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希望这可以帮助你

于 2013-07-05T14:14:30.447 回答