iPhone: How to Close MFMailComposeViewController? - iphone

I'm having difficulties closing an email message that I have raised.
The email opens nicely, but once it is opened it will not close as the mailComposeController:mailer didFinishWithResult:result error:error handler never gets invoked.
As far as I can tell I have all the bits in place to be able to do this.
Anyone any ideas of what I can look at?
Here is how I raise the email:
-(IBAction)emailButtonPressed
{
NSString *text = #"My Email Text";
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.delegate = self;
[mailer setSubject:#"Note"];
[mailer setMessageBody:text isHTML:NO];
[self presentModalViewController:mailer animated:YES];
[mailer release];
}
and later in the class I have this code to handle the close (but it never gets called):
-(void)mailComposeController:(MFMailComposeViewController *)mailer didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self becomeFirstResponder];
[mailer dismissModalViewControllerAnimated:YES];
}
My header file is defined as:
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
#interface myViewController : UIViewController <UIActionSheetDelegate, UIAlertViewDelegate, MFMailComposeViewControllerDelegate, UINavigationControllerDelegate>
Thanks
Iphaaw

You are setting the delegate wrong, the delegate property in MFMailComposeViewController is called mailComposeDelegate, so it should be:
mailer.mailComposeDelegate = self;
Another possible error I can see is calling dismissModalViewControllerAnimated: on mailer - you should send this message to the view controller who presented the mail interface - self in this case:
[self dismissModalViewControllerAnimated:YES];
I wrote "possible error" because it might actually work if iOS routes the message through responder chain, anyway - the documentation says it should be send to presenter.

Related

How can i call a mail function in Root ViewController?

I want to send a mail with attachment . In my app i created a separate class for the mail function and in rootViewController I have button Email It . When i click it My mail Function has to be called but it doesn't.
MFMailViewController *controller = [[MFMailViewController alloc] initWithNibName:"MFMailViewController" bundle:nil];
[[self navigationController] pushViewController:viewController animated:YES];
[viewController release];
After that i got know that i cant use the mail function like this. can any one help me find this
Its not MFMailViewController, its MFMailComposeViewController. Please go through this Apple reference to get more details.
Here is a sample code
if([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
if(mailController!=nil) {
mailController.mailComposeDelegate = self;
[mailController setSubject:urSubject];
[mailController setMessageBody:urBody isHTML:NO];
[mailController addAttachmentData:urData mimeType:type fileName:urfileName];
}
[self presentModalViewController:mailController animated:YES];
[mailController release];
}
Create an instance method in MFMailViewController class with some parameter like dictionary or NSArray
when you click on Email it button you are creating object of MFMailViewController then called the instance method using the controller object and pass the parameter or info to.
then push it's view to navigation.
#import <Foundation/Foundation.h>
#interface MFMailViewController : NSObject
{}
-(void)sendEmail:(NSMutableDictionary*)withData;
#end
#import "MFMailViewController.h"
#implementation MFMailViewController
-(void)sendEmail:(NSMutableDictionary*)withData;
{
//definition goes here.................
}
#end
MFMailViewController *controller = [[MFMailViewController alloc] initWithNibName:"MFMailViewController" bundle:nil];
[controller sendEmail:withData];
[[self navigationController] pushViewController:viewController animated:YES];
[viewController release];
simple where you want to send mail just define this code in you method .....
but first define delegate in .h file of your viewController
delegate is MFMailComposeViewControllerDelegate
and then in button click method write this code......
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setToRecipients:#"Recipients Name"];
[controller setSubject:#"your subject"];
[controller setMessageBody:#"Your Text Here for Mail" isHTML:YES];
if (controller)
{
[self presentModalViewController:controller animated:YES];
[controller release];
}
and then you can just paste bellow mailComposerDelegate method in yourViewController.m file.....
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error;
{
if (result == MFMailComposeResultSent) {
NSLog(#"Mail Sent");
}
[self dismissModalViewControllerAnimated:YES];
}

Need help on mail composer in xcode

I have warning on with error message below. Please help.
Assigning to 'id<MFMailComposeViewControllerDeelegate>' from incompatible type 'MyViewController'
and
Class 'MyViewController' does not implement the 'MFMailComposeViewControllerDelegate' protocol
The code is
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
//below bold code is warning
**picker.mailComposeDelegate = self;**
[picker setSubject:#"My first apps!"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObjects: nil];
[picker setToRecipients:toRecipients];
[picker setMessageBody:TextView.text isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
The problem is as the error says: your class MyViewController doesn't conform to the MFMailComposeViewControllerDelegate protocol. Your interface line should look something like this:
#interface MyViewController : UIViewController <MFMailComposeViewControllerDelegate>
And, of course, you should be sure to implement mailComposeController:didFinishWithResult:error: in your class.

iPhone In App Email Issue

when I press the sendMail button, it will go to the mail button, but when I hit send or cancel it will not take me back to my application. Any suggestions?
-(IBAction)sendMail {
MFMailComposeViewController *mailComposer = [[[MFMailComposeViewController alloc] init] autorelease] ;
if ([MFMailComposeViewController canSendMail]) {
[mailComposer setToRecipients:nil];
[mailComposer setSubject:nil];
[mailComposer setMessageBody:#"Default text" isHTML:NO];
[self presentModalViewController:mailComposer animated:YES];
}
}
You need to set a delegate (generally the same view controller that presented the MFMailComposeViewController). Then when the user taps the Save or Cancel button the MFMailComposeViewController will call -mailComposeController:didFinishWithResult:error on the delegate. So set yourself as the delegate and define the following method:
#pragma mark -
#pragma mark MessageUI Delegate Methods
- (void)mailComposeController:(MFMailComposeViewController*)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError*)error {
[controller dismissModalViewControllerAnimated:YES];
}
Add the following line below your mail composer initialization
mailComposer.mailComposeDelegate = self;//very important if you want feedbacks on what the user did with your email sheet.
Then implement the delegate method as Kenny suggested. You can use this method to take custom actions.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
{
//Do something, If you need to
}
break;
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}
Remember to conform to the delegate by adding
#interface YourViewController : UIViewController <MFMailComposeViewControllerDelegate> { }
If you are still having trouble, You may visit the following tutorial where everything nicely explained:
http://blog.mugunthkumar.com/coding/iphone-tutorial-in-app-email/
This wonderful line:
mailComposer.mailComposeDelegate = self;
is what made ​​it happen for days without knowing that was what went wrong.
And do not forget them:
# import <UIKit/UIKit.h>
# import <MessageUI/MessageUI.h>
# import <MessageUI/MFMailComposeViewController.h>
In addition to importing the MessageUI.framework in the project.
Verified in IOS5

UIScrollView and presentModalViewController

I add UITableViewController in UIScrollView. When I call
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Email"];
[picker setMessageBody:currentcelltext isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
MFMailComposeViewController is coming in UIScrollView. So, I can't press cancel and send button.
How to fix it ?
I hope that you set MessageUI.framework in your xcode project..
After you have to set delegate in .h file..
than ,
you have to create one more method..
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self becomeFirstResponder];
[self dismissModalViewControllerAnimated:YES];
}
You might have fixed the issues.
This answer is for those who might come to this post with the expectation of answer.
To fix this you have to use delegation pattern. I assume all the pages in your scrollview are viewcontroller instances and scrollview is subview to viewcontroller.
This answer https://stackoverflow.com/a/626946/451867 can help you to implement delegate pattern in your project,
You can read developer docs for more info - docs

MFMailComposeViewController does not send mail

I am trying to send mail using MFMailComposeViewController. Everything works, except that mails do not get sent, and I always get MFMailComposeResultFailed.
Any pointers? I am NOT using the simulator, and sending mail does work from my device. I do have a connection (testing via Reachability), and [MFMailComposeViewController canSendMail] returns YES.
No compiler warnings in the project, no crashes...
It was a bug in IOS4.
I had both an Exchange mail account and an old, inactive IMAP account on my phone. Apparently, that leads to problems with iOS4. The mails actually were stuck in the outbox. Once I removed the inactive IMAP account, everything worked as expected.
Some readers might be facing this problem:
Make sure you implement the <MFMailComposeViewControllerDelegate> protocol
Here's the code:
// in TestViewController.h
#interface TestViewController : UIViewController<MFMailComposeViewControllerDelegate>
#end
// in TestViewController.m
#interface TestViewController ()
#end
#implementation
- (void) compose {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Hello there"];
[picker setToRecipients:#[]];
// Fill out the email body text
NSString *emailBody = #"Hello, sending a message from my app";
[picker setMessageBody:emailBody isHTML:NO];
// use this function. presentModalViewController:... is deprecated
[self presentViewController:picker animated:YES completion:nil];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
It's difficult to tell without seeing a code snippet, however you should check the following:
1) you have correctly set the MFMailComposeViewController's delegate and implemented its delegate methods;
2) you have set the mail subject using setSubject:
3) you have set the message body using setMessageBody:isHTML:
and optionally set an attach using addAttachmentData:mimeType:fileName:
4) you have presented to the user your mail compose view controller using something like
[self presentModalViewController:mcvc animated:YES];
Hope this helps.