如何将标签的文本作为电子邮件附件发送?

时间:2012-11-14 20:48:33

标签: iphone ios xcode sdk email-attachments

我想在电子邮件中附加标签文字, 如何在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];
    }
}

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

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

你快到了。

- 您从文件中读取文本并附加

===

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

NSData *data = [self.label.text dataUsingEncoding:NSUTF8Encoding];

答案 1 :(得分:0)

您可以将标签文本添加到正文中。

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

类似这样的事情