NSString new line doesn't work in mail? - iphone

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

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

Related

how to attach any document in email in iphone app

I am sending email from iphone app it is working fine but i want that with email i should attached a pdf file which is documents folder of the app.for testing first i attached a png from resources folder of app but it does not get attached and not sent in email i am using following code.
- (IBAction)onEmailResult
{
if ([[MFMailComposeViewController class] canSendMail]) {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Pig Game"];
[picker setToRecipients:toRecipients];
int a=10;
int b=100;
NSString *path = [[NSBundle mainBundle] pathForResource:#"project existing photo" ofType:#"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:#"png" fileName:#"icon.png"];
NSString * emailBody = [NSString stringWithFormat:#"My Score %d",a];
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
else {
int a=10;
int b=20;
NSString *recipients = #"mailto:imran_husain_2000#yahoo.com?&subject=Pig Game";
NSString *body = [NSString stringWithFormat:#"&body=My Score: %d/%d, My Time: %#", a,b, time];
NSString *email = [NSString stringWithFormat:#"%#%#", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
}
Try following code snippet.
- (NSString *)pathForFile : (NSString *) fileName{
return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: fileName];
}
- (void)sendMailWithAttachedFile:(NSString *) fileName extention:(NSString *) extension{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
// NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:[self pathForResourse:fileName ofType:extension]];
NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:[self pathForFile:[NSString stringWithFormat:#"%#.%#", fileName, extension]]];
NSData *data=[[NSData alloc]initWithContentsOfURL:outputURL];
[picker addAttachmentData:data mimeType:#"application/pdf" fileName:#"TestOne.pdf"];
[self presentViewController:picker animated:YES completion:nil];
}
Now Call Send Mail method As:
[self sendMailWithAttachedFile:#"TestOne" :#"pdf"];
-(void)displayMailComposerSheet
{
NSData *soundFile = [[NSData alloc] initWithContentsOfURL:YourDocumentFile];
[mail addAttachmentData:soundFile mimeType:#".txt" fileName:#"YourDocumentFile.txt"];
}
This code implement in displayMailComposerSheet i hope this code is useful for You
MFMailComposeViewController *picker1 = [[MFMailComposeViewController alloc] init];
NSArray *recipentsArray = [[NSArray alloc]initWithObjects:[shopDictValues objectForKey:#"vEmail"], nil];
picker1.mailComposeDelegate = self;
picker1.modalPresentationStyle = UIModalPresentationFormSheet;
[picker1 setSubject:#"Subject"];
[picker1 setToRecipients:recipentsArray];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSMutableArray *arydefault = [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults] valueForKey:#"jacketCount"]];
for (int i=0;i<arydefault.count;i++)
{
NSString *pdfFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#%#.pdf",[[arydefault objectAtIndex:i]valueForKey:#"productlinename"],[arydefault objectAtIndex:i]]];
[picker1 addAttachmentData:[NSData dataWithContentsOfFile:pdfFilePath] mimeType:#"application/pdf" fileName:[NSString stringWithFormat:#"Order - %#",[[[NSString stringWithFormat:#"%#%i",[[arr objectAtIndex:i]valueForKey:#"productlinename"],i] componentsSeparatedByCharactersInSet: [[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:#""]]];
}
NSString *emailBody = #"Email Body goes here.";
[picker1 setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:picker1 animated:YES];

Attaching a mp3 file to email directly from app?

I am working on an app where, if the user selects a sound, they can email it to themselves from the app.
The attachment part 'appears' to work, however, when I send the email, the recipient has no attachment?
On the iPad/iPhone itself, it looks like it is attaching it when it comes to compose, but it is not working? :/
Here is the code I am using;
- (void)onSend:(id)sender{
int nIndex;
UIButton *btnSender = (UIButton *)sender;
NSLog( #"%d", btnSender.tag );
for ( int i = 0; i < [ m_aryFileName count ]; i++ ) {
if( i == ( btnSender.tag - 100 ) ){
nIndex = i;
}
}
NSString *strFileName = [ m_aryFileName objectAtIndex:nIndex ];
strFileName = [ strFileName stringByAppendingString:#".mp3" ];
NSData* nData = [ NSData dataWithContentsOfFile:strFileName ];
MFMailComposeViewController *pickerMail = [[MFMailComposeViewController alloc] init];
pickerMail.mailComposeDelegate = self;
[pickerMail setSubject:#"myMail Attachment"];
// Attach an image to the email
[pickerMail addAttachmentData:nData mimeType:#"audio/mp3" fileName:strFileName ];
// Fill out the email body text
NSString *emailBody = #"Here is your attachment";
[pickerMail setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:pickerMail animated:YES];
[pickerMail release];
}
try this code mate,
NSString *strFileName = [m_aryFileName objectAtIndex:nIndex];
strFileName = [strFileName stringByAppendingString:#".mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:strFileName];
NSData *nData = [[NSData alloc] initWithContentsOfURL:fileURL];
MFMailComposeViewController *pickerMail = [[MFMailComposeViewController alloc] init];
pickerMail.mailComposeDelegate = self;
[pickerMail setSubject:#"myMail Attachment"];
[pickerMail addAttachmentData:nData mimeType:#"audio/mpeg" fileName:strFileName ];
NSString *emailBody = #"Here is your attachment";
[pickerMail setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:pickerMail animated:YES];
Below code might be useful to you:
NSData *videoData = [NSData dataWithContentsOfURL:mediaUrl];
[mailcomposer addAttachmentData:videoData mimeType:#"video/mp4" fileName:#"Video"]
Try using a mime type of audio/mpeg instead. You can get this value by running the following code:
#import <MobileCoreServices/MobileCoreServices.h>
CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
(__bridge CFStringRef)#"mp3",
NULL);
CFStringRef mimeTags = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType);
CFRelease(uti);
NSString *mediaType = [NSString stringWithString:(__bridge NSString *)mimeTags];
CFRelease(mimeTags);
How does that work?
if ([MFMailComposeViewController canSendMail] && m_pDataArray != nil)
{
NSString * pSongPath = [[NSBundle mainBundle] pathForResource:#"song" ofType"#"mp3"]; ;//get the file
MFMailComposeViewController * pMailComposer = [[MFMailComposeViewController alloc] init];
pMailComposer.mailComposeDelegate = self;
[pMailComposer setMessageBody:#"msg body" isHTML:NO];
NSURL * pFileUrl = [[[NSURL alloc] initFileURLWithPath:pSongPath] autorelease];
NSData * pData = [[[NSData alloc] initWithContentsOfURL:pFileUrl] autorelease];
[pMailComposer addAttachmentData:pData mimeType:#"audio/mpeg" fileName:#"song.mp3" ]];
[self presentModalViewController:pMailComposer animated:YES];
[pMailComposer release];
}
else
{
UIAlertView *pAlert = [[UIAlertView alloc] initWithTitle:#"Failure" message:#"Your device doesn't support the composer sheet" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[pAlert show];
[pAlert release];
pAlert = nil;
}
be sure that the file should have valid extension like .mp3 , in that case it will work properly

How to Send image in Email body?

I am sending image with some text in Email Body using this link my code is given below
NSString *urlString = #"url";
NSString *searchingurl = [NSString stringWithFormat:#"%#%#", urlString,idnumber];
NSString *string = [NSString stringWithFormat:#"%#<br><br>Item No :%#<br>Type :%#<br>Size : %#",searchingurl,str123,mytype,itemsize];
NSMutableString *emailBody = [[[NSMutableString alloc] initWithString:#"<html><body>"] retain];
NSData *imageData = UIImageJPEGRepresentation(myImage, 1.0f);
NSString *encodedString = [imageData base64Encoding];
[emailBody appendString:[NSString stringWithFormat:#"<p><b><img src='data:image/png;base64,%#'></b></p>",encodedString]];
[emailBody appendString:string];
[emailBody appendString:#"</body></html>"];
NSLog(#"%#",emailBody);
MFMailComposeViewController *emailDialog = [[MFMailComposeViewController alloc] init];
emailDialog.mailComposeDelegate = self;
[emailDialog setSubject:#""];
[emailDialog setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:emailDialog animated:YES];
[emailDialog release];
[emailBody release];
Screenshow for above code
After Sending Mail to Yahoo it show my image and text on both Mackbook and in iphone device , when i send mail to Gmail and checked it in iphone device it show image with text but When i checked gmail in Mackbook it not show my image
Some one Can guide me why my image not show in Gmail on PC.Any help will be appriated.Thanks
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Your subject"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:#""];
//NSArray *ccRecipients = [NSArray arrayWithObjects:#"second#example.com", #"third#example.com", nil];
//NSArray *bccRecipients = [NSArray arrayWithObject:#"fourth#example.com"];
[picker setToRecipients:toRecipients];
//[picker setCcRecipients:ccRecipients];
//[picker setBccRecipients:bccRecipients];
NSLog(#"Video size >> %d",(videodta.length/1024)/1024);
[picker addAttachmentData:imageData mimeType:#"image/png" fileName:#"testapp"];
// Fill out the email body text
NSString *emailBody = #"Type your message here";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
}

how to send the mail to the desired user from iphone app

I am sending mail from iphone application
i want that instead of this static reception user should enter in to field in mail and mail should be sent to them.
- (void)onEmailResult
{
if ([[MFMailComposeViewController class] canSendMail]) {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Game"];
// Set up recipients
NSArray * toRecipients = [NSArray arrayWithObject:#"husain_2000#yahoo.com"];
[picker setToRecipients:toRecipients];
// Fill out the email body text
NSString * emailBody = [NSString stringWithFormat:#"My Score: %d/%d, My Time: %#", numberOfCorrectAnswer, [[[DataManager sharedInstance] questions] count], time];
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
else {
NSString *recipients = #"mailto:husain_2000#yahoo.com?&subject=Game";
NSString *body = [NSString stringWithFormat:#"&body=My Score: %d/%d, My Time: %#", numberOfCorrectAnswer, [[[DataManager sharedInstance] questions] count], time];
NSString *email = [NSString stringWithFormat:#"%#%#", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
}
The email should be configured in the iPhone.
Then Try this code
- (void)onEmailResult
{
if ([[MFMailComposeViewController class] canSendMail]) {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Game"];
// Leave recipients as blank
// Fill out the email body text
NSString * emailBody = [NSString stringWithFormat:#"My Score: %d/%d, My Time: %#", numberOfCorrectAnswer, [[[DataManager sharedInstance] questions] count], time];
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
}

Mail Composer Problem with app ondevice method

I am using this code for mail
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSString *msgTitle = #"Sample Title";
[picker setSubject:msgTitle];
NSArray *toRecipients =[[NSArray alloc] init];
NSArray *ccRecipients =[[NSArray alloc] init];
NSArray *bccRecipients =[[NSArray alloc] init];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
NSString *sum = #"The Email Body string is here";
NSString *emailBody;
emailBody = [NSString stringWithFormat:#"%#",sum];
[picker setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:picker animated:YES];
[picker release];
it works,
i want to know,is it required to modify
-(void)launchMailAppOnDevice
{
NSString *recipients = #"mailto:first#example.com?cc=second#example.com,third#example.com&subject=Hello from California!";
NSString *body = #"&body=It is raining in sunny California!";
NSString *email = [NSString stringWithFormat:#"%#%#", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
if yes then
how i modify in this method.
Please help me.
If email address is given statically and displayed in To address.
You can use this code,
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail]) {
[mail setToRecipients:[NSArray arrayWithObjects:#"AAA#BBB.com",nil]];
[self presentModalViewController:mail animated:YES];
}
[mail release];
And
see my answer
Best of luck.