I am trying to add MFMailComposer to my iPhone app and not having much luck getting it to launch in my iPhone 2G. It always launches the email app to the accounts page and closes my app. Works like a charm in the simulator.
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self displayComposerSheet];
}
-(void)displayComposerSheet
{
Class mailClass = (NSClassFromString(#"MFMailComposeViewController"));
if (mailClass != nil)
{
// you have the MFMailComposeViewController class
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSArray *mailArr = [[NSArray alloc] initWithObjects:self.sendTo,nil];
[picker setSubject:#"Hello from iCamp America"];
[picker setToRecipients:mailArr];
NSString *emailBody = #"\n\n\email created with iCamp America\nwww.iCampAmerica.com";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
[mailArr release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"You cannot send an email !" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
The MFMailComposeViewController is only available on iPhone OS > 3.0. Are you running that version your development phone?
Silly me - I found that I had conflicting calls to send the email - I was calling BOTH the mailTo:URL and the MFMailComposer methods with the same action trigger.....
Related
EDIT: I eventually contacted Apple DTS. After I provided a stackshot from an affected user, DTS decided I should file a bug with Apple BugReporter. So, at this point, I think it's an issue with MFMailComposer, but it's unresolved. The Apple bug number is 13602051
I have a bug that has been coming up again and again in an app.
Some users who upgrade their iOS version report that they can no longer use the email export in my app, which uses MFMailComposer. The app freezes, and doesn't generate a crash report.
My code is pretty simple, and I can't reproduce the reported bug, but many users have now said this happens after an iOS update. Here is the code:
// using ARC, so no reference counting
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
#autoreleasepool {
if (gpxFilePath) {
NSData *gpx = [NSData dataWithContentsOfFile:gpxFilePath];
[controller addAttachmentData:gpx mimeType:#"text/gpx" fileName:[self cleanFileName]];
gpx = nil;
}
}
[controller setSubject:subject];
[controller setMessageBody:body isHTML:YES];
[[MAP_APP_DELEGATE mainController] presentModalViewController:controller animated:YES];
After this is called, the email view comes up, but then is unresponsive.
I'm using the next code for iOS 6.1 and it works for me.
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc]init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"subject"];
User *user = [user_array objectAtIndex:1];
NSArray *toRecipients = [NSArray arrayWithObjects:#"mail address", nil];
[mailer setToRecipients:toRecipients];
NSArray *cc = [NSArray arrayWithObjects:#"mail address", nil];
[mailer setCcRecipients:cc];
NSDictionary *dic = [one array objectAtIndex:0];
NSString *description = [dic objectForKey:#"Description"];
NSString *emailBody = description;
[mailer setMessageBody:emailBody isHTML:NO];
[self presentViewController:mailer animated:YES completion:nil];
[mailer release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
Remember that simulator is not able to send e-mail, so alert view will be shown in this case.
Note 1: presentModalViewController is deprecated in iOS 6.0
Note 2: try to send the email without data to check if this is what causes the issue
This should solve your problem.
[[MAP_APP_DELEGATE mainController] presentModalViewController:controller animated:YES];
controller = nil;
I am developing an application, i want to know how to achive forget password scenario in iPhone.
When user forget their password then I have a button in my app when i click on it UIAlertView open in which i have a textfield, user must enter their email address and the password get mail on that mail id.
How can I do that I have define a action for button this is the code:
-(IBAction)forgetpassword:(id)sender
{
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"Forget Password" message:#"Please Enter your Email address " delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
av.alertViewStyle = UIAlertViewStylePlainTextInput;
[av textFieldAtIndex:0].delegate = self;
[av show];
}
but what i need is just require the code to which i can use and mail the password to the email id which user will entered.
just add delegate MFMessageComposeViewControllerDelegate to the .h file and then use this code when you want to email and also add framework MessageUI.framework in the project
-(IBAction)forgetpassword:(id)sender
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
NSString *mailBody = #"your Message";
[mailComposeViewController setMessageBody:mailBody isHTML:NO];
mailComposeViewController.mailComposeDelegate = self;
[self presentViewController:mailComposeViewController animated:YES completion:nil];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"e-Mail Sending Alert"
message:#"You can't send a mail"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
and this bellow method is delegate method of MFMessageComposeViewControllerDelegate
#pragma mark - MFMessage Delegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
if (result == MFMailComposeResultSent)
{
NSLog(#"\n\n Email Sent");
}
[self dismissViewControllerAnimated:YES completion:nil];
}
you can also use SKPSMTPmessage web-service for send e-mail
i hope this help you...
In my app I am using MFMailComposer. It crashes when I send mail without mail configuration (without having entered mail ID and password in the Mail app on the device).
This is the line that causes the crash:
[self presentModalViewController:picker animated:YES];
-(void) showEmailModalView
{
NSLog(#"Start method <ExportStatisticsController> : <showEmailModalView> --");
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self; // <- very important step if you want feedbacks on what the
//user did with your email sheet
[picker setSubject:SendEmailSubject];
NSArray *torec=[[NSArray alloc] initWithObjects:SendEmailToEmailID, nil];
[picker setToRecipients:torec];
[torec release];
//------ message body ---
NSString *body =#"";
body=[NSString stringWithFormat:#"%# From : %#\n",body, emailTextField.text];
body=[NSString stringWithFormat:#"%# Subject : %#\n",body,subjectTextField.text];
//email contents
body = [NSString stringWithFormat:#"%# Message : \n %#", body,messageBodyTextView.text];
[picker setMessageBody:body isHTML:NO]; // depends. Mostly YES, unless you want to send it as plain text (boring)
picker.navigationBar.barStyle = UIBarStyleBlack; // choose your style, unfortunately, Translucent colors behave quirky.
[self presentModalViewController:picker animated:YES];
[picker release];
NSLog(#"End method <ExportStatisticsController> : <showEmailModalView> --");
}
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
NSLog(#"Start method <ExportStatisticsController> : <didFinishWithResult> --");
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Message MFMailComposeResultCancelled");
break;
case MFMailComposeResultSaved:
NSLog(#"Message MFMailComposeResultSaved");
break;
case MFMailComposeResultSent:
NSLog(#"Message sent Successfuly");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Email" message:#"Mail Sent Successfully!"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
break;
case MFMailComposeResultFailed:
NSLog(#"Message MFMailComposeResultFailed");
break;
default:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Email" message:#"Sending Failed - Unknown Error :-("
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
break;
}
[self dismissModalViewControllerAnimated:YES];
NSLog(#"End method <ExportStatisticsController> : <didFinishWithResult> --");
}
You should call
[MFMailComposeViewController canSendMail]
before presenting the view controller, eg
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
[self presentModalViewController:composer];
} else {
// Show error message maybe
}
That's true. You will have to configure your mail app and check whether the device can send mail or not. Since if it would have been possible without mail app then we would be able to send mail via simulator (which I think is not possible). The receiver of your mail must know from where he/she is getting the mail and I think the sender cannot be set from the code I might be wrong but these are my view since I was struggling the same situation.
Hope it helps.
I am using the MFMailComposeViewController Controller like this:
MFMailComposeViewController *picker1 = [[MFMailComposeViewController alloc] init];
picker1.mailComposeDelegate = self;
[picker1 setSubject:#"I have a pencil for you"];
UIImage *roboPic = [UIImage imageNamed:#"RobotWithPencil.jpg"];
NSData *imageData = UIImageJPEGRepresentation(roboPic, 1);
[picker addAttachmentData:imageData mimeType:#"image/jpg" fileName:#"RobotWithPencil.jpg"];
NSString *emailBody = #"This is a cool image of a robot I found. Check it out!";
[picker1 setMessageBody:emailBody isHTML:YES];
picker1.navigationBar.barStyle = UIBarStyleBlack;
[self presentModalViewController:picker1 animated:YES];
[picker1 release];
When I press the "Cancel" button I don't see the panel with "Draft", "Save Draft" and "Cancel" buttons, the screen is locked/frozen but the panel with buttons mentioned above doesn't appear.
I will be glad to get any assistance.
Thanks in advance
Moshe
you have to use this for mail composing. and in interface file write this delegte MFMailComposeViewControllerDelegate . also import messageUi framework. its working.
#pragma mark Compose Mail
// Displays an email composition interface inside the application. Populates all the Mail fields.
-(void)displayComposer
{
if(![MFMailComposeViewController canSendMail])
{
[self setUpMailAccount];
return;
}
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Strpy's Revange"];
[[picker navigationBar] setTintColor:[UIColor blackColor]];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:#""];
NSArray *ccRecipients = [NSArray arrayWithObjects:#"", #"", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:#""];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
// Fill out the email body text
if([UIAppDelegate gameCodeFlag]==0)
{
NSString *emailBody = [NSString stringWithFormat:#"Game Score! %d",[UIAppDelegate scorePost]];
[picker setMessageBody:emailBody isHTML:NO];
}
else {
[UIAppDelegate readFriendPlist];
NSString *emailBody = [NSString stringWithFormat:#"Game Code! %#",[UIAppDelegate gameCode]];
[picker setMessageBody:emailBody isHTML:NO];
}
[self presentModalViewController:picker animated:YES];
[picker release];
}
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSaved:
break;
case MFMailComposeResultSent:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Sending..."
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
break;
}
case MFMailComposeResultFailed:
break;
default:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Email" message:#"Sending Failed - Unknown Error :-("
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
break;
}
[self dismissModalViewControllerAnimated:YES];
if([UIAppDelegate gameCodeFlag]==1)
{
[[CCDirector sharedDirector] pushScene:[StorePage scene]];
}
}
I would like to add the text "Sent from " to the bottom of the message
if([MFMailComposeViewController canSendMail]){
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:[NSString stringWithFormat:#"A message from: %#",self.profileName]];
[controller setMessageBody:[NSString stringWithFormat:#"%#",self.cellBody] isHTML:NO];
[self presentModalViewController:controller animated:YES];
[controller release];
}
else {
UIAlertView *alertView;
alertView = [[UIAlertView alloc] initWithTitle:#"E-Mail Not Enabled" message:#"E-Mail is not supported on this device" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
You could set the message body as isHTML:YES and use line breaks <br> or try using \n in the setMessageBody string
Then just add your "sent from" message after that.
Try out this code I have changed the code as per your requirement
if([MFMailComposeViewController canSendMail]){
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:[NSString stringWithFormat:#"A message from: %#",self.profileName]];
[controller setMessageBody:[NSString stringWithFormat:#"%#\nSent from : %#",self.cellBody,self.profileName] isHTML:NO];
[self presentModalViewController:controller animated:YES];
[controller release];
}
else {
UIAlertView *alertView;
alertView = [[UIAlertView alloc] initWithTitle:#"E-Mail Not Enabled" message:#"E-Mail is not supported on this device" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
Happy Coding...