Search code examples
iphonensstringmfmailcomposer

NSString new line doesn't work in mail?


I have two UITextViews that I want to be displayed when the user sends mail. The problem is that I want the text from the second textview to be displayed in a new line in the mail.

I've tried with NSLog and it works just fine there, is there a problem with the mail API or why is not working properly?

My code looks like this:

NSString *desc = descriptionTextView.text;
NSString *ingredients = ingredientsTextView.text;
NSString *emailBody = [NSString stringWithFormat:@"%@\n\n%@", desc, ingredients];
NSLog(@"%@\n\n%@", desc, ingredients);

-(void)displayComposerSheet 

{ MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; picker.mailComposeDelegate = self;

[picker setSubject:titleLabel.text];
NSString *desc = descriptionTextView.text;
NSString *ingredients = ingredientsTextView.text;
NSString *emailBody = [NSString stringWithFormat:@"%@\n\n%@", desc, ingredients];
NSLog(@"%@\n\n%@", desc, ingredients);
[picker setMessageBody:emailBody isHTML:YES];


[self presentModalViewController:picker animated:YES];
[picker release];

}

- (void)viewDidLoad {
titleLabel.text = [recipeData valueForKey:@"title"];
descriptionTextView.text = [recipeData valueForKey:@"description"];
ingredientsTextView.text = [recipeData valueForKey:@"ingredients"];
[super viewDidLoad];

}


Solution

  • In mail composing just use the html tags then it works fine.
    Use a html <br /> tag for a new line.

    NSString *desc = descriptionTextView.text;
    NSString *ingredients = ingredientsTextView.text;
    NSString *emailBody = [NSString stringWithFormat:@"%@ <br/> <br/> %@", desc, ingredients];
    // [email setMessageBody:emailBody isHTML:YES]; 
    NSLog(@"%@\n\n%@", desc, ingredients);