Reputation: 236
I seem to have fallen fowl of something that seems like it should be very simple!
How do I now populate an email's body with the strings a user has entered into NSUserDefaults?
Basically there is a button else ware that enters a sting such as 'YES' into NSUserDetaults. How do I make the email body those stored strings.
This is the code I currently have:
- (IBAction)buttonPressed:(id)sender {
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:@"SYS"];
[controller setMessageBody:@"How do I make this the content of NSUSERDEFAULTS?" isHTML:NO];
controller.navigationBar.barStyle = UIBarStyleBlack;
[self presentViewController:controller animated:YES completion:nil];
Any help would be greatly appreciated!
Thanks!
Using your code the strings appear as : Yes, No, Maybe, No way,
How can I alter the format to make them appear
Yes,
No,
Maybe,
No way.
Thanks again for your time!
Upvotes: 0
Views: 368
Reputation: 7170
What you want is a line break character, which can be achieved with \n
(newline).
NSArray *storedStrings = [[NSUserDefaults standardUserDefaults] objectForKey:@"yourStoredKey"];
NSMutableString *messageBody = [NSMutableString string];
for (NSString *aString in storedStrings) {
[messageBody appendFormat:@"%@,\n\n", aString];
}
[messageBody deleteCharactersInRange:NSMakeRange(messageBody.length-3, 3)];
[controller setMessageBody:messageBody isHTML:NO];
That line I added right after the for loop will remove the last three characters from the string, i.e. the two newline characters and the comma.
Upvotes: 1
Reputation: 1522
Try this method
- (IBAction)buttonPressed:(id)sender {
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:@"SYS"];
[controller setMessageBody:[[nsstring stringwithformat@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"yourStoredKey"]] isHTML:NO];
controller.navigationBar.barStyle = UIBarStyleBlack;
[self presentViewController:controller animated:YES completion:nil];
Upvotes: 0