0

我想将标签文本附加到电子邮件中,如何将 UILabel 中的文本作为电子邮件的附件发送?

这是我正在使用的代码:

-(IBAction)send:(id)sender {

    if ([MFMailComposeViewController canSendMail])
    {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 300, 100)];
        label.numberOfLines = 0;
        label.textAlignment = UITextAlignmentCenter;
        label.text = @"text";

        [self.view addSubview:label];
        [label release];

        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];

        mailer.mailComposeDelegate = self;
        [mailer setSubject:@"Subject"];

        NSString *fileName = @"my file.txt";
        NSArray  *paths =   NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];

        NSData *myData = [NSData dataWithContentsOfFile:path];
        [mailer addAttachmentData:myData mimeType:@"text/plain" fileName:fileName];

        // Fill out the email body text
        NSString *emailBody = @"Email Body";

        [mailer setMessageBody:emailBody isHTML:NO];

        [self presentModalViewController:mailer animated:YES];

        [mailer release];
    }
    else
    {
        UIAlertView *alertm = [[UIAlertView alloc] initWithTitle:@"Failure"
                                                         message:@"Please make sure that your   email application is open"
                                                        delegate:nil
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles: nil];
        [alertm show];
        [alertm release];
    }
}

如何链接该标签以将其附加到电子邮件?

任何帮助将不胜感激。

4

2 回答 2

0

You can add the Label text to the Body.

        NSString *emailBody = [NSString stringWithFormat:@"Your Voice File Attached. %@", label.text];

something like this

于 2012-11-14T21:23:30.570 回答
0

您快到了。

-- 你从文件中读取文本并附加它

===

不是从文件中读取数据,而是从标签本身读取数据

NSData *data = [self.label.text dataUsingEncoding:NSUTF8Encoding];
于 2012-11-14T21:15:31.570 回答