xcodeuitableviewcore-datamfmailcomposeviewcontroller

Best way to populate e-mail body from uitableview


Ok let me try to make it a better question.

I have a tableViewController in my app containing some info that the user would like to email someone with that data from the tableview.

I could retain an array with that information.

The tough part of the question is how do i populate the body of the message with the data from my tableview using objective-c language ?

Do I have to make a huge string containing all the html code ? Or is there a better/easier way around it ? Even if it is a simple looking table just so the client will send that out to someone.

I guess any solution/advice could be a great waypoint for me to know how should i work with this.


Solution

  • I freaking nailed it ! If you guys ever want to compose an email having the data from your UITableViewController here's how i made it. Just remember to import your data header file...

    #import Recipe.h //in the implementation file
    #import Ingredients.h //in the implementation file
    #import <MessageUI/MFMailComposeViewController.h> // this line gotta be in the header file
    
    -(IBAction)sendmail{
    MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
    [composer setMailComposeDelegate:self];
    NSString *recipeTitle = @"<h5>Recipe name: ";
    recipeTitle = [recipeTitle stringByAppendingFormat:recipe.name];
    recipeTitle = [recipeTitle stringByAppendingFormat:@"</h5>"];
    
    NSString *ingredientAmount = @"";
    NSString *ingredientAisle = @"";
    NSString *ingredientTitle = @"";
    
    NSString *tableFirstLine = @"<table width='300' border='1'><tr><td>Ingredient</td><td>Amount</td><td>Aisle</td></tr>";
    NSString *increments = @"";
    increments = [increments stringByAppendingFormat:recipeTitle];
    increments = [increments stringByAppendingFormat:tableFirstLine];
    int i;
    
    for (i=0; i < [ingredients count]; i++) {
        Ingredient *ingredient = [ingredients objectAtIndex:i];
        ingredientTitle = ingredient.name;
        ingredientAmount = ingredient.amount;
        ingredientAisle = ingredient.aisle;
    
        increments = [increments stringByAppendingFormat:@"<tr><td>"];
        increments = [increments stringByAppendingFormat:ingredientTitle];
        increments = [increments stringByAppendingFormat:@"</td><td>"];
        increments = [increments stringByAppendingFormat:ingredientAmount];
        increments = [increments stringByAppendingFormat:@"</td><td>"];
        increments = [increments stringByAppendingFormat:ingredientAisle];
        increments = [increments stringByAppendingFormat:@"</td></tr>"];
        if (i == [ingredients count]) {
            //IF THIS IS THE LAST INGREDIENT, CLOSE THE TABLE
            increments = [increments stringByAppendingFormat:@"</table>"];
        }
    }
    
    NSLog(@"CODE:: %@", increments);
    
    if ([MFMailComposeViewController canSendMail]) {
        [composer setToRecipients:[NSArray arrayWithObjects:@"123@abc.com", nil]];
        [composer setSubject:@"subject here"];
        [composer setMessageBody:increments isHTML:YES];
        [composer setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
        [self presentModalViewController:composer animated:YES];
        [composer release];
    }else {
        [composer release];
        }
    }
    

    This one was a huge step for me and it is actually very useful if you want to create interesting (basic) tools in your app. Thanks to everyone in this quintessential website for objective-c programmers.

    Andthis is the result you get. Simple but a good way to start.

    enter image description here