How to change message details (email body) in MFMailComposeViewController? - iphone

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!

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.

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];

Related

MessageUI framework issue for UITextField contents are not displayed

I've included messageUI framework in my app for sending mail.It contains five UITextField where the values entered by user should be displayed in mail body. Here is my code
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:[NSString stringWithFormat:#"Appointment From Mr/Mrs. %#",text1.text]];
NSArray *toRecipients = [NSArray arrayWithObjects:#"info#xxx.com", nil];
[mailer setToRecipients:toRecipients];
NSString *emailBody =[NSString stringWithFormat:#"Name :%#\nDate: %#\nPreferred Time Slot: %#\nE-Mail:%#\nSpecific Requests:",text1.text,text2.text,text3.text,text4.text,text5.text];
[mailer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailer animated:YES];
[mailer release];
}
The issue i'm facing is that all the four text field values are displayed only the fifth text field values are not displayed..Any idea?...am i missing anything?...
You only have four %# in the form at string, thus the last parameter supplied to the format method is ignored.
Change the format string to have five parameters:
NSString *emailBody =[NSString stringWithFormat:#"Name :%#\nDate: %#\nPreferred Time Slot: %#\nE-Mail:%#\nSpecific Requests:%2",text1.text,text2.text,text3.text,text4.text,text5.text];
Edit the following line of code
NSString *emailBody =[NSString stringWithFormat:#"Name :%#\nDate: %#\nPreferred Time Slot: %#\nE-Mail:%#\nSpecific Requests:%#",text1.text,text2.text,text3.text,text4.text,text5.text];
you will get your requirements...Dont forget to use resignFirstresponder to hide keyboard after u entered text...

iPhone SMS Url Scheme with Body Content?

I am looking to pre-fill the SMS body with content given "smsto:555-555-5555:Hello World"
I have done my research and everybody says it cannot be done but I have a QR reader on my phone right now that can take "sms:555-555-5555:Hello world" and pre-fills the content with the correct phone number scanned.
So my question is. How do i do this? it is pretty clear that the URL scheme provided doesn't handle the body. What other methods are out there to pre-fill the body? it can clearly be done with the QuickMark Reader.
You can use the modalViewController for SMS to fill the body of a text.
add the MessageUI framework to your project.
set you viewController as the MFMessageComposeDelegate
Create the modal view and present it:
-(void) showMessageComposerWithText:(NSString*)messageText telNumber:(NSString*)telNumber composeDelegate:(id)delegate
{
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText]){
controller.body = messageText;
controller.recipients = [NSArray arrayWithObject:telNumber];
controller.messageComposeDelegate = delegate;
[delegate presentModalViewController:controller animated:YES];
}
}
You can't launch the Messages app with a predefined message according to Apple Documentation.
What you can do is implement the handling yourself and parse the URL like the following:
NSURL *url = /* the url you get from the web (in webview delegate) or after QR Code scan */;
if ([url.scheme isEqualToString:#"sms"]) // is it a sms ?
{
if([MFMessageComposeViewController canSendText]) // can I send text message ?
{
NSArray *parts = [url.absoluteString componentsSeparatedByString:#":"];
NSString *messageText = parts.count == 3 ? [parts objectAtIndex:2] : #"";
NSString *telNumber = parts.count >= 2 ? [parts objectAtIndex:1] : #"";
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
controller.body = messageText;
controller.recipients = [NSArray arrayWithObject:telNumber];
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
[controller release];
}
}
And voilĂ  !

MFMailComposersheet does not allocates in device iOS 5.1 version

I have the following line of code,
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:APP_NAME];
[picker addAttachmentData:pdfData mimeType:#"pdf" fileName:pdfFileName];
NSString *emailBody = #"";
[picker setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:picker animated:YES];
But iPad just stuck at the first line of the code.
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
When i put the break point on the another line it does not come to the second line of code it is just stuck and does not open MailCompserSheet and it is hung.
This problem appears only on iOS version 5.1 in iPad.
So anyway have idea what to do?
I guess you might not have configured mail in your iPad.
So before initializing the class MFMailComposeViewController, call MFMailComposeViewController's + (BOOL)canSendMail to check whether you can be able to send mail with the device or not.

Problem with MFMailComposeViewController - User Can't Edit Mail Body - iPhone

I usually use the below code to allow the user to submit feedback on my apps. However for some reason in my OpenGL app the below code has a problem. It opens the email form correctly, however the form is locked - i.e the user can't actually edit the body of the text. Can anybody spot why this is happening ?
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Feedback on Stop That Bomb Free !"];
NSArray *toRecipients = [NSArray arrayWithObject:#"anemail#gmail.com"];
[picker setToRecipients:toRecipients];
// Fill out the email body text
NSString *emailBody =
[NSString stringWithFormat:#"Hi Martin, I would like to make the following comment : "];
[picker setMessageBody:emailBody isHTML:YES];
picker.navigationBar.barStyle = UIBarStyleBlack;
[inputController presentModalViewController:picker animated:YES];
[picker release];
From reading your code I can find some things you should change:
Replace:
NSString *emailBody = [NSString stringWithFormat:#"Hi Martin, I would like to make the following comment : "];
With:
NSString *emailBody = #"Hi Martin, I would like to make the following comment : ";
As you are not using any formatting; you dont need to call the class method to create the simple string.
The other thing you can change is the fact that you message does not contain HTML.
So you dont need isHTML:YES.
I have tested this successfully on a sample app.
I imagine the problem is with the view controller presenting the view, rather than the messageUI view.

New line and returns ignored in setMessageBody

Am I doing something dumb? I can pre-fill and email ok but the "\r\n" is ignored in the emailBody:
- (void) sendEventInEmail
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSString *emailSubject = [eventDictionary objectForKey:EVENT_NAME_KEY];
[picker setSubject:emailSubject];
// Fill out the email body text
NSString *iTunesLink = #"http://itunes.apple.com/gb/app/whats-on-reading/id347859140?mt=8"; // Link to iTune App link
NSString *content = [eventDictionary objectForKey:#"Description"];
NSString *emailBody = [NSString stringWithFormat:#"%#\r\nSent using <a href = '%#'>What's On Reading</a> for the iPhone.", content, iTunesLink];
[picker setMessageBody:emailBody isHTML:YES];
picker.navigationBar.barStyle = UIBarStyleBlack;
[self presentModalViewController:picker animated:YES];
[picker release];
}
Regards
Dave
If isHTML set YES \n does not work you either have to set isHTML:NO or use a HTML line break such as <br /> to enter a new line.
<p> </p> inserts a new paragraph which usually means double line break.
Try this with isHTML:YES:
[picker setMessageBody:#"1st line<br />2nd line<br />3rd line<br />and so on..." isHTML:YES];
If isHTML:NO just put \n
[picker setMessageBody:#"1st line\n2nd line\n3rd line\nand so on..." isHTML:NO];
It will give you this:
1st line2nd line3rd lineand so on...
Doh... been working too hard and mixing Objective C strings with HTML. Used <p> and </p> tags to fix.
Dave