I am trying to add email functionality to my app. I can get the MFMailComposeViewController to display correctly and pre-populate its subject and body, but for some reason when the user clicks on the "Cancel" or "Send" buttons in the nav bar the app just hangs. I inserted a NSLog() statement into the first line of mailComposeController"didFinishWithResult:error and it doesn't even print that line out to the console.
Does anybody have an idea what would cause the MFMailComposeViewController to hang like that?
Here is my code from the header:
#import "ManagedObjectEditor.h"
#import <MessageUI/MessageUI.h>
#interface MyManagedObjectEditor : ManagedObjectEditor
<MFMailComposeViewControllerDelegate, UIImagePickerControllerDelegate,
UINavigationControllerDelegate> {
}
- (IBAction)emailObject;
#end
from the implementation file:
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.delegate = self;
[mailComposer setSubject:NSLocalizedString(#"An email from me",
#"An email from me")];
[mailComposer setMessageBody:emailString
isHTML:YES];
[self presentModalViewController:mailComposer animated:YES];
[mailComposer release];
}
[error release];
[emailString release];
and here is the code from the callback:
#pragma mark -
#pragma mark Mail Compose Delegate Methods
- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError *)error {
NSLog(#"in didFinishWithResult:");
switch (result) {
case MFMailComposeResultCancelled:
NSLog(#"cancelled");
break;
case MFMailComposeResultSaved:
NSLog(#"saved");
break;
case MFMailComposeResultSent:
NSLog(#"sent");
break;
case MFMailComposeResultFailed: {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Error sending email!",#"Error sending email!")
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:NSLocalizedString(#"Bummer",#"Bummer")
otherButtonTitles:nil];
[alert show];
[alert release];
break;
}
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}
Thanks!
I got bit by this too, you need to set the mailComposeDelegate, not the delegate.
Related
I am having a trouble using MFMailComposeViewController.
When I click on a button, an instance of MFMailComposeViewController should present itself:
http://postimg.org/image/i24gn4oi7/
But when it does, this happens:
http://postimg.org/image/kdwm0isrl/
Here's the code:
- (IBAction)actionEmailComposer {
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
NSMutableArray *email = [[NSMutableArray alloc] initWithObjects:#"factto#gmail.com", nil];
[mc setToRecipients:email];
//[mc setSubject:#"SUBJECT_HERE"];
[mc setMessageBody:#"ReferĂȘncia: \nCor:" isHTML:NO];
mc.modalPresentationStyle = UIModalPresentationFullScreen;
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved");
break;
case MFMailComposeResultSent:
{
NSLog(#"Mail sent");
UIAlertView *msg = [[UIAlertView alloc] initWithTitle:#"Obrigado!" message:#"Logo logo entraremos em contato!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[msg show];
}
break;
case MFMailComposeResultFailed:
NSLog(#"Mail sent failure: %#", [error localizedDescription]);
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
Actually, on the iPad it does work properly, it just happens on the iPhone!
What is going on? What am I doing wrong?
Thanks in advance.
The view controller that you present the mail view controller from needs to be full screen and at the front, if it isn't some parts of the view won't respond properly to touches and / or be properly visible (as you see).
I use MFMailComposeViewController to send mail in my app. But when present mail compose view controller, all of navigation buttons are disabled (except back button in select mail address screen), i must use Home button to quit app. Does anyone has idea?
Here is screen shot:
Code:
- (void)shareVieEmail
{
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:#"Test subject"];
[mailViewController setMessageBody:#"Mail message body" isHTML:NO];
NSData *imageData = [NSData dataWithContentsOfFile:photourl];
[mailViewController addAttachmentData:imageData mimeType:#"image/jpg" fileName:#"example_photo"];
[self presentModalViewController:mailViewController animated:YES];
} else {
[[[UIAlertView alloc] initWithTitle:#"Cannot send mail" message:#"Device is unable to send email in its current state" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] show];
}
}
Delegate method :
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
//NSLog(#"Result: canceled");
break;
case MFMailComposeResultSaved:
//NSLog(#"Result: saved");
break;
case MFMailComposeResultSent:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Result" message:#"Mail Sent Successfully" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
break;
case MFMailComposeResultFailed:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Result" message:#"Mail Sent Failed" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
break;
default:
//NSLog(#"Result: not sent");
break;
}
if (error) {
[[[UIAlertView alloc] initWithTitle:#"Cannot send mail" message:[NSString stringWithFormat:#"ERROR:%#", [error userInfo]] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil] show];
}
[self dismissModalViewControllerAnimated:YES];
}
And in header file, I declared implement MFMailComposeViewControllerDelegate.
I had exactly the same problem. and it took me a while to figure this out but no surprise it came down to customized UIBarButtonItem
I bet in your UIBarButtonItem.h there is a method
-(void)setEnabled:(BOOL)enabled ;
and the implementation looks like this:
-(void)setEnabled:(BOOL)enabled {
if (self.customView) {
if ([[self.customView.subviews objectAtIndex:0] isKindOfClass:[UIButton class]]) {
((UIButton*)[self.customView.subviews objectAtIndex:0]).enabled = enabled;
}
}
}
and this is causing problem so as soon as you comment out this method your problem should go away.
I also had this problem, but it my case it was because I had overridden setNavigationBarHidden:animated: from UINavigationController as proposed in this workaround for a bug in CNContactViewController. One solution that would still include the workaround and solve the problem in MFMailComposeViewController would be to use method swizzling to be able to call either the original method or the overridden one, depending on the class of the current topViewController.
In your MFMailComposeViewController's delegate you need to implement didFinishWithResult: and dismiss the modal view controller from there.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// you can test the result of the mail sending here if you want
[self dismissModalViewControllerAnimated:YES];
}
For swift 4.0+
func mailComposeController(controller: MFMailComposeViewController,
didFinishWithResult result: MFMailComposeResult, error: NSError?) {
// Check the result or perform other tasks.
// Dismiss the mail compose view controller.
controller.dismissViewControllerAnimated(true, completion: nil)
}
I used the sample code on the Apple Dev site to learn how to set pre-composed emails, but is there a way to set precomposed SMS messages, similarly?
First you have to add the framework MessageUI to your project and import the library "MessageUI/MessageUI.h". Then conform to the protocol <MFMessageComposeViewControllerDelegate>.
Now to send an SMS:
- (IBAction) sendSMS:(id)sender
{
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
controller.body = #"The body of the SMS you want";
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
[controller release];
}
To catch the result of the sending operation:
- (void) messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch(result)
{
case MessageComposeResultCancelled: break; //handle cancelled event
case MessageComposeResultFailed: break; //handle failed event
case MessageComposeResultSent: break; //handle sent event
}
[self dismissModalViewControllerAnimated:YES];
}
The body property on MFMessageComposeViewController allows you to set the message body just like you can for an email.
Check out the documentation: http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMessageComposeViewController_class/Reference/Reference.html
See this article on Apple's Dev Center:
Sending an SMS Message
PresentModalViewController is now deprecated in IOS 6. So i used
[self presentViewController:controller animated:YES completion:nil];
whole code is as following
-(IBAction)sendSMSButtonTouchupInside:(id)sender
{
MFMessageComposeViewController *controller =
[[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
controller.body = #"Whatever you want";
controller.recipients = [NSArray arrayWithObjects:#"03136602888", nil];
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"MyApp" message:#"Unknown Error"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
switch (result)
{
case MessageComposeResultCancelled:
NSLog(#"Cancelled");
[alert show];
break;
case MessageComposeResultFailed:
[alert show];
break;
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
I am doing this its working in simulator but when we try to open in device then program is terminating.
Plz suggess me fast.
MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
mail.mailComposeDelegate=self;
[mail setToRecipients:[NSArray arrayWithObjects:#"marketing#realestateinvestar.com.au",nil]];
//[self becomeFirstResponder];
mail.navigationBar.tintColor=[UIColor blackColor];
[self presentModalViewController:mail animated:YES];
if ([MFMessageComposeViewController canSendText])
Your problem is here. You are trying to check whether device will be able to send Text messages and not email Message. you should try using
if([MFMailComposeViewController canSendMail])
The problem might be that your device is not configured to any accounts in the mail.Please check this once.
Have you implement MFMailComposeViewControllerdelegate methods in your code??
#pragma mark --------------------------------------------
#pragma mark MFMailComposeViewController delegate Methods
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
switch (result) {
case MFMailComposeResultCancelled:
NSLog(#"Mail send canceled.");
/*
Execute your code for canceled event here ...
*/
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved.");
/*
Execute your code for email saved event here ...
*/
break;
case MFMailComposeResultSent: {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Mail Sent" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.tag = 1;
alert.delegate = self;
[alert show];
[alert release];
break;
}
case MFMailComposeResultFailed: {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Mail Sending Failed" message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.tag = 2;
alert.delegate = self;
[alert show];
[alert release];
break;
}
default:
break;
}
[controller dismissModalViewControllerAnimated:YES];//dismissing modal view controller
}
Your code looks ok, but did you check if the device can send mail:
if ([MFMailComposeViewController canSendText]) {
MFMailComposeViewController *mail=[[MFMailComposeViewController alloc]init];
mail.mailComposeDelegate=self;
[mail setToRecipients:[NSArray arrayWithObjects:#"marketing#realestateinvestar.com.au",nil]];
mail.navigationBar.tintColor=[UIColor blackColor];
[self presentModalViewController:mail animated:YES];
[mail release], mail = nil;
} else {
// show message to the use that he can't send an email.
}
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.