I have an app that allows the user to send a test email from their iPhone. My app calls a method to activate the compose mail function like this:
-(void)displayComposerSheet
{
//set up a way to cancel the email here
//picker is an instance of MSMailComposeViewController already declared in the .h file
[picker setSubject:@"Test Mail"];
// Set up recipients
// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:@"Icon" ofType:@"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"Icon"];
// Fill out the email body text
NSString *emailBody = @"This is a test mail.";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
NSLog(@"mail is working");
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
emailLabel.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
emailLabel.text = @"Mail sending canceled.";
break;
case MFMailComposeResultSaved:
emailLabel.text = @"Mail saved.";
break;
case MFMailComposeResultSent:
{
emailLabel.text = @"Mail sent.";
NSLog(@"It's away!");
UIAlertView *emailAlertView = [[UIAlertView alloc] initWithTitle:@"Sent!" message:@"Mail sent successfully." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[emailAlertView show];
[self dismissModalViewControllerAnimated:YES];
[self.navigationController popViewControllerAnimated:YES];
}
break;
case MFMailComposeResultFailed:
{
emailLabel.text = @"Mail sending failed.";
}
break;
default:
{
emailLabel.text = @"Mail not sent.";
}
break;
}
}
My problem is that when the compose email function is active, I am unable to come out of this function and return to my app. The only way out of this is by actually going ahead and sending a message. There is a default “cancel” bar button that appears on the top left hand corner of the navigation bar, which when clicked, gives me three options: “delete draft”, “save draft”, and “cancel”. If I select “delete draft”, it does nothing except to return me to the compose message screen. Is there a way for me to allow the user to return to the app after starting the compose mail function, but prior to sending it? Is there a way to add extra functionality to the “cancel” bar button to accomplish this?
Thanks in advance to all who reply.
You have to implement
MFMessageComposeViewControllerDelegatewith the- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)resultmethod.You will dismiss your message view in this method.
EDIT : i confused with
MFMailComposeViewControllerDelegatebut the purpose is the same as withMFMessageComposeViewControllerDelegate