Search code examples
objective-ccocoaios5nsstringmfmailcomposeviewcontroller

How to add NSString's to email using setMessageBody


I'm trying to implement an autofilled email. I have the emailviewcontroller popping into place correctly in a separate test environment, but I cant work out how to add strings to the message body. Basically I have three buttons on the root controller of the gui that set the values of three strings. All I want is for those three strings to be be reproduce (if possible separated by commas) in the message body. Here is my code so far:

    -(IBAction)displayComposerSheet
    {

    NSArray *recipient = [NSArray arrayWithObjects:@"[email protected]", nil];

    if([MFMailComposeViewController canSendMail])
    {
            MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc]             init];
        mailViewController.mailComposeDelegate = self;
        [mailViewController setSubject:@"Place holder ... this is a test subject"];
        [mailViewController setMessageBody: isHTML:NO];
        [mailViewController setToRecipients:recipient];


        [self presentModalViewController:mailViewController animated:YES];
    }
    else
        NSLog(@"Device is unable to send email in its current state");

    }

    -(void)mailComposeController:(MFMailComposeViewController *)controller     didFinishWithREsult:(MFMailComposeResult)result error:(NSError *)error
{
    [self dismissModalViewControllerAnimated:YES];
}
- (IBAction)addString1:(id)sender {

    NSString *string1;
    string1 = @"teststring1";

}

- (IBAction)addString2:(id)sender {

    NSString *string2;
    string2 = @"teststring2";
}

- (IBAction)addString3:(id)sender {
    NSString *string3;
    string3 = @"teststring3";
}

Solution

  • First of all, you'll want to declare your strings in your .h file so that you can use them throughout the .m

    @interface ViewController : UIViewController
    {
        NSString *string1;
        NSString *string2;
        NSString *string3;
    }
    

    Then in the mail composer you'll use a formatted string to display your strings in sequence (escaped by %@)

        [mailViewController setMessageBody:[NSString stringWithFormat:@"%@, %@, %@",string1,string2,string3] isHTML:NO];
    

    Of course after declaring your strings in your header file there will be no need to declare them locally so change your IBActions to look like this:

       - (IBAction)addString1:(id)sender 
        {
            string1 = @"teststring1";
        }
    
        - (IBAction)addString2:(id)sender 
        {
            string2 = @"teststring2";
        }
    
        - (IBAction)addString3:(id)sender 
        {
            string3 = @"teststring3";
        }
    

    Additionally, to avoid unwanted instances of (null) being inserted into your message body I recommend you give these strings an initial blank value in viewDidLoad. This way the will start out without any content, but they won't be nil.

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        string1 = @"";
        string2 = @"";
        string3 = @"";
    }