MFMailComposeViewController loads a blank white screen - iphone

I'm testing on an iPod Touch running OS 3.1.3
Trying to allow users to send an email from within the app - but when the following code is executed, the entire screen just turns completely blank / white.
Any ideas on why this is happening?
I've got the MessageUI framework in the project.
I'm importing and delegating in the header file:
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
<MFMailComposeViewControllerDelegate>
And here's the code, pretty standard:
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"App Feedback"];
[picker setToRecipients:[NSArray arrayWithObject:#"xyz#gmail.com"]];
[self presentModalViewController:picker animated:YES];
[picker release];
}
And then I have the didFinishWithResult function that would dismiss the ModalViewController when the email has been sent.
But again, all I get is a blank white screen on my iPod Touch. =/
Thanks!

if([MFMailComposeViewController canSendMail]){
MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
mail.mailComposeDelegate=self;
[mail setSubject:#"App Feedback"];
[mail setMessageBody:#"*your message content*" isHTML:NO];
[self presentModalViewController:mail animated:YES];
[mail release];
}
- (void)mailComposeController:(MFMailComposeViewController*)mailController didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self dismissModalViewControllerAnimated:YES];
}

You can take look at sample code from apple:
http://developer.apple.com/library/ios/#samplecode/MessageComposer/Listings/Classes_MessageComposerViewController_m.html
-(IBAction)showMailPicker:(id)sender {
Class mailClass = (NSClassFromString(#"MFMailComposeViewController"));
if (mailClass != nil) {
[self displayMailComposerSheet];
if ([mailClass canSendMail]) {
[self displayMailComposerSheet];
}
else {
feedbackMsg.hidden = NO;
feedbackMsg.text = #"Device not configured to send mail.";
}
}
else {
feedbackMsg.hidden = NO;
feedbackMsg.text = #"Device not configured to send mail.";
}
}
-(void)displayMailComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Hello from California!"];
NSArray *toRecipients = [NSArray arrayWithObject:#"first#example.com"];
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];
NSString *path = [[NSBundle mainBundle] pathForResource:#"rainy" ofType:#"jpg"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:#"image/jpeg" fileName:#"rainy"];
NSString *emailBody = #"It is raining in sunny California!";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
feedbackMsg.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
feedbackMsg.text = #"Result: Mail sending canceled";
break;
case MFMailComposeResultSaved:
feedbackMsg.text = #"Result: Mail saved";
break;
case MFMailComposeResultSent:
feedbackMsg.text = #"Result: Mail sent";
break;
case MFMailComposeResultFailed:
feedbackMsg.text = #"Result: Mail sending failed";
break;
default:
feedbackMsg.text = #"Result: Mail not sent";
break;
}
[self dismissModalViewControllerAnimated:YES];
}

Related

MFMailComposeViewController not dismiss

I used MFMailComposeViewController for sending mail to others. When click a button the compose sheet is opening, and i can able to type To address,subject,message body. But after clicking send button the mail page is not closing.
code:
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"My image"];
UIImage *myImage = [UIImage imageNamed:#"mobiletuts-logo.png"];
NSData *imageData = UIImagePNGRepresentation(myImage);
[mailer addAttachmentData:imageData mimeType:#"image/png" fileName:#"Image"];
NSString *emailBody = #"Hi, my image";
[mailer setMessageBody:emailBody isHTML:NO];
[self presentViewController:mailer animated:YES completion:nil];
}
- (void)mailComposeController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch (result) {
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSent:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
Sorry. Forget to add these lines. After this lines mail working
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self dismissViewControllerAnimated:YES completion:nil];
}
Try this
[self presentModalViewController:mailer animated:YES];
for dismiss
[self dismissModalViewControllerAnimated:YES];

Sending html text to mail as an attachment in ios as pdf format

i have to send html text to mail as an attachment with a pdf extension.
Code :
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
NSArray *toReceipents =[NSArray arrayWithObjects:#"", nil];
[mailViewController setToRecipients:toReceipents];
[mailViewController setSubject:strMailMessage];
NSLog(#"Mail Message:%# %#",strMailMessage,appDelegate.strShareText);
NSData* data = [appDelegate.strShareText dataUsingEncoding:NSUTF8StringEncoding];
[mailViewController setMessageBody:appDelegate.strShareText isHTML:YES];
[mailViewController addAttachmentData:data mimeType:#"application/pdf" fileName:#"Medication file.pdf"];
[self presentModalViewController:mailViewController animated:YES];
[mailViewController release];
Note:when i download the pdf file i am getting the same text.but i want to show the text in table format in pdf document
Good code but for HTML try this ;) :
- (IBAction)mailCompose:(id)sender {
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
[mail setSubject:#"Hello World!"];
NSArray *toRecipients = [NSArray arrayWithObjects:#"e-mail here or leve empty", nil];
[mail setToRecipients:toRecipients];
NSString *emailBody = #"Body message App</b><br /><a href='https://itunes.apple.com/it/app/yourApp/id'>Download Free on AppStore!</a>";
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"myVoice.caf"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
[mail addAttachmentData:myData mimeType:documentsDirectory fileName:#"myVoice.caf"];
[mail setMessageBody:emailBody isHTML:YES];
mail.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentViewController:mail animated:YES completion:nil];
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
switch (result) {
case MFMailComposeResultCancelled:
NSLog(#"Cancelled");
break;
case MFMailComposeResultSaved:
NSLog(#"Saved");
break;
case MFMailComposeResultFailed:
NSLog(#"Faild");
break;
case MFMailComposeResultSent:
NSLog(#"Sent");
break;
default:
NSLog(#"Default");
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
Enjy hope this can help you

how to set email Receptionit a NSString in iphone application

I am sending email from iphone app and i want that email sent to should be the value or address the var emailTO has should be in TO automatically.
NSString*emailTO=#"ali#yahoo.com";
[picker setRecipients:[NSArray arrayWithObject:emailTO]];
but it does not work any idea how to implement in this way thanks
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setToRecipients:[NSArray arrayWithObject:[NSString stringWithFormat:#"%#,#"ur Receipent data"]]]];
Try to use this one...
NSArray *array = [NSArray arrayWithObject:#"ali#yahoo.com"];
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setToRecipients:array];
[self presentViewController:mailViewController animated:YES completion:NULL];
}
else
{
NSLog(#"Device is unable to send email in its current state.");
}
Try This. Hope will Help You.Note that you will need to add the MessageUI framework to your app.
#import <MessageUI/MessageUI.h>
and
MFMailComposeViewController *mail = [[[MFMailComposeViewController alloc] init] autorelease];
mail.mailComposeDelegate = self;
[mail setToRecipients:[NSArray arrayWithObject:#"email#gmail.com"]];
[mail setSubject:#"Set Your Subject Here"];
[self presentModalViewController:mail animated:YES];
Use MFMailComposeViewControllerDelegate protocol: Delegate Function for conform the result
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Result: canceled");
break;
case MFMailComposeResultSaved:
NSLog(#"Result: saved");
break;
case MFMailComposeResultSent:
NSLog(#"Result: sent");
break;
case MFMailComposeResultFailed:
NSLog(#"Result: failed");
break;
default:
NSLog(#"Result: not sent");
break;
}
}
Description
setToRecipients:
Sets the initial recipients to include in the email’s “To” field.
- (void)setToRecipients:(NSArray*)toRecipients
Parameters
toRecipients
An array of NSString objects, each of which contains the email address of a single recipient.
try this
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[picker setRecipients:[NSArray arrayWithObject:#"your email address"]];
[self presentViewController:mailViewController animated:YES completion:nil];
If you want to open iPhone Email application, then use below code.So, you have not any need to add MFMailComposeViewController.
NSString *subject = #"";
NSString *body = #"";
NSString *address = #"test#gmail.com";
NSString *cc = #"";
NSString *path = [NSString stringWithFormat:#"mailto:%#?cc=%#&subject=%#&body=%#", address, cc, subject, body];
NSURL *url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; [[UIApplication sharedApplication] openURL:url];
You should use
[picker setRecipients:[NSArray arrayWithObjects:emailTO,nil]];

iphone: How to add Email functionality in iPhone Ebook App

I m working on iPhone eBook app.
In my app books are created using HTML.
In Index.html page i have to put the email functionality which email(send) the contents of particular chapter.
Now by using webview i show the index.html and in that i have created UIActionSheet which will show the Email button.
Please suggest me that how can i identify the index of different links to send
email of particular chapter.
Below code will work even if you have not configured email in your device.
Here is the code:
- (IBAction) sendEmail:(id)sender
{
Class mailClass = (NSClassFromString(#"MFMailComposeViewController"));
if (mailClass != nil)
{
// We must always check whether the current device is configured for sending emails
if ([mailClass canSendMail])
{
[self displayComposerSheet];
}
else
{
[self launchMailAppOnDevice];
}
}
else
{
[self launchMailAppOnDevice];
}
}
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Hello from DShah!"];
NSArray *toRecipients = [NSArray arrayWithObject:#"first#example.com"];
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];
NSString *path = [[NSBundle mainBundle] pathForResource:#"userdata" ofType:#"abc"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.abc", #"userdata"]];
NSData *myData = [NSData dataWithContentsOfFile:fullPath];
[picker addAttachmentData:myData mimeType:#"csv" fileName:#"userdata.abc"];
NSString *emailBody = #"It is raining in sunny California!";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
-(void)launchMailAppOnDevice
{
NSString *recipients = #"mailto:first#example.com&subject=Hello from DShah!";
NSString *body = #"&body=It is cold in Winter!";
NSString *email = [NSString stringWithFormat:#"%#%#", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
Then implement the Delegate method as below....
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
message = #"Result: canceled";
break;
case MFMailComposeResultSaved:
message = #"Result: saved";
break;
case MFMailComposeResultSent:
message = #"Result: sent";
break;
case MFMailComposeResultFailed:
message = #"Result: failed";
break;
default:
message = #"Result: not sent";
break;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Email Demo" message:message delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
[self dismissModalViewControllerAnimated:YES];
}
Check out Apple's MailComposer example. This will show you everything you need. http://developer.apple.com/library/ios/#samplecode/MailComposer/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008865

How to send mail from iphone app?

I am try to send mail from my app. I want to type(dynamically) recipient id,ccid,sub and message.
Thanks
Senthilkumar
on iOS 3+
Import #import in your view controller header.
Then:
-(void)showMailPanel {
MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
// only on iOS < 3
//if ([MFMailComposeViewController canSendMail] == NO)
// [self launchMailApp]; // you need to
mailComposeViewController.mailComposeDelegate = self;
[mailComposeViewController setToRecipients:[NSArray arrayWithObjects:#"email address1",#"email address 2",nil]];
[mailComposeViewController setSubject:#"your subject"];
[mailComposeViewController setMessageBody:#"your body" isHTML:YES];
mailComposeViewController.delegate = self;
[self.navigationController presentModalViewController:mailComposeViewController animated:YES];
[mailComposeViewController release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Result: canceled");
break;
case MFMailComposeResultSaved:
NSLog(#"Result: saved");
break;
case MFMailComposeResultSent:
NSLog(#"Result: sent");
break;
case MFMailComposeResultFailed:
NSLog(#"Result: failed");
break;
default:
NSLog(#"Result: not sent");
break;
}
[controller dismissModalViewControllerAnimated:YES];
}
-(void)launchMailApp {
{
NSString *recipients = #"dest_email";
NSString *email = [NSString stringWithFormat:#"%#%#", recipients, subject];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
You need things like this:
//Import this in your .h file
#import <MessageUI/MessageUI.h>
...
MFMailComposeViewController *mailController = [[[MFMailComposeViewController alloc] init] autorelease];
mailController.mailComposeDelegate = self;
[mailController setSubject:[NSString stringWithFormat:#"Report a problem - #%#", yourUserID]];
[mailController setToRecipients:[NSArray arrayWithObject:#"support#yourWebsite.com"]];
[self presentModalViewController:mailController animated:YES];
Besides, the parent view controller (the delegate) needs to conform to the MFMailComposeViewControllerDelegate protocol:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
//Extra implementation here...
[self dismissModalViewControllerAnimated:YES];
}