Unable to send mail using MFMailComposeViewController in iOS - ios

I got an error :
viewServiceDidTerminateWithError: Error Domain=_UIViewServiceInterfaceErrorDomain Code=3 "The operation couldn’t be completed. (_UIViewServiceInterfaceErrorDomain error 3.)" UserInfo=0x7c3f4600 {Message=Service Connection Interrupted}
if([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
[mail setSubject:#"Contact"];
[mail setMessageBody:#"Here is some main text in the email!" isHTML:NO];
[mail setToRecipients:#[self.salonemail.text]];
[self presentViewController:mail animated:YES completion:NULL];
}
else{
[self showAlert:#"Looks like your device can't send emails"];
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:NULL];
}

Have you tested on real devices? This usually comes in iPhone simulators.

Related

Mock the MFMailComposeViewController on iOS simulator. Just need to display the composer

I just need to display the email composer on my iOS simulator (No need to send the actual mail).
As soon as I am initializing the MFMailComposeViewController I am getting alert pop - up saying that email account is not set.
This is what I am doing.
MFMailComposeViewController* composeVC = [[MFMailComposeViewController alloc] init];
This line pops up the alert controller.
For simulator email configuration is not available.
Use the below code to send email.
- (void)openEmail
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mail =
[[MFMailComposeViewController alloc] init];
mail.mailComposeDelegate = self;
[mail setSubject:#"Subject"];
[mail setMessageBody:#"body message" isHTML:NO];
[mail setToRecipients:#[#"email1#example.com",#"email2#example.com"]];
/// if attachment then implement below line
//[mail addAttachmentData:<#(nonnull NSData *)#> mimeType:<#(nonnull NSString *)#> fileName:<#(nonnull NSString *)#>]
[self presentViewController:mail animated:YES completion:NULL];
}
else {
NSLog(#"Please configure your email. Settings->Accounts & Passwords->Add Account");
}
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(nullable NSError *)error
{
switch (result) {
case MFMailComposeResultSent:
NSLog(#"Sent");
break;
default:
break;
}
}

MFMailComposeViewController not dismiss with Cancel button

I've a class that subclass NSObject with a function do display a MFMailComposeViewController. Here is the code :
MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
mailController.mailComposeDelegate = self;
[mailController setSubject:#"Sample Subject"];
[mailController setMessageBody:#"Here is some main text in the email!" isHTML:NO];
[mailController setToRecipients:#[self.email]];
UITabBarController *tabbarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
UINavigationController *navigationController = tabbarController.selectedViewController;
[navigationController.topViewController presentViewController:mailController animated:YES completion:NULL];
Everything works well with this code. The problem is when I want to dismiss the MFMailComposeViewController. Sometime I get a crash, sometimes it just don't happens nothing. I've implemented the delegate function :
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
UITabBarController *tabbarController = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
UINavigationController *navigationController = tabbarController.selectedViewController;
[navigationController.topViewController dismissViewControllerAnimated:YES completion:nil];
}
After that I tried to show and dismiss it directlty from a ViewController and everything was working. Even the cancel button.
I don't know why it works in my ViewController Class but not in my subclass of NSObject.
When I get the crash I've seen in the logs :
-[MFMailComposeInternalViewController _notifyCompositionDidFinish]
Try this,
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
// Do your work and dismiss after you are done with it.
[controller dismissViewControllerAnimated:YES completion:nil];
}
Hope that helps.
Try this in your singleton class
UIViewController *currentViewController;
- (void)sendEmail:(id) viewController {
currentViewController=(UIViewController*)viewController;
NSString * appVersionString =#"";
NSString *strEmailMessage=#"";
NSString *strEmailSubject=#"";
NSArray *toRecipents =#"";
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate =viewController;
[mc setSubject:strEmailSubject];
[mc setMessageBody:strEmailMessage isHTML:NO];
[mc setToRecipients:toRecipents];
[viewController presentViewController:mc animated:YES completion:NULL];
}
else{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Error" message:#"Please setup email account" delegate:nil cancelButtonTitle:#"cancle" otherButtonTitles:nil];
[alert show];
}
}
set delegate like this
mc.mailComposeDelegate =viewController;
to dissmiss viewController
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSaved:
break;
case MFMailComposeResultSent:
break;
case MFMailComposeResultFailed:
break;
}
// Close the Mail Interface
[currentViewController dismissViewControllerAnimated:YES completion:NULL];
}
i hope this will work...

MailCompositionService quit unexpectedly ios

I am new to this,
I have this code in my viewController,
- (void)sendMail:(id)sender {
NSArray *to = [NSArray arrayWithObjects:#"rayjada11#gmail.com", nil];
mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
[mailComposer setToRecipients:to];
[mailComposer setSubject:#"Test Mail"];
[mailComposer setMessageBody:#"Testing message body" isHTML:NO];
[self presentModalViewController:mailComposer animated:YES];
}
#pragma mark - mail compose delegate
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
if(result) {
NSLog(#"Result = %d", result);
}
if(error) {
NSLog(#"Error = %#", error);
}
[self dismissModalViewControllerAnimated:YES];
}
But when I click send button in my controller, I am getting error like,
2016-09-05 14:55:24.488 mailDemo[1276:104171]
viewServiceDidTerminateWithError: Error
Domain=_UIViewServiceInterfaceErrorDomain Code=3 "(null)"
UserInfo={Message=Service Connection Interrupted} 2016-09-05
14:55:24.989 mailDemo[1276:104171] Trying to dismiss the presentation
controller while transitioning already.
(<_UIFullscreenPresentationController: 0x7fe35b52d2a0>) 2016-09-05
14:55:24.991 mailDemo[1276:104171] transitionViewForCurrentTransition
is not set, presentation controller was dismissed during the
presentation? (<_UIFullscreenPresentationController: 0x7fe35b52d2a0>)
What is the issue?
There is nothing wrong with your code.MFMailComposeViewController is not work for simulator, Try same code in real device.
simulator doesn't supported method and device doesn't login in mail that this method nothing response.
replace this method ::
- (void)sendMail:(id)sender {
if (![MFMailComposeViewController canSendMail]) {
NSLog(#"Mail services are not available.");
return;
}
else{
NSArray *to = [NSArray arrayWithObjects:#"rayjada11#gmail.com", nil];
mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
[mailComposer setToRecipients:to];
[mailComposer setSubject:#"Test Mail"];
[mailComposer setMessageBody:#"Testing message body" isHTML:NO];
[self presentModalViewController:mailComposer animated:YES];
}
}
MFMailComposeViewController does not work for simulators. If you try the same code in real device, it will work. There is nothing wrong with your code.

MFMailComposeViewController Keeps Closing Automatically

This is a really weird issue! I have a button on screen which when pressed launches the MFMailComposeViewController using the following code:
-(IBAction) openComposeEmailScreen:(id) sender {
if([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
mailComposeViewController.mailComposeDelegate = self;
[mailComposeViewController setToRecipients:[NSArray arrayWithObject:#"johndoe#gmail.com"]];
[mailComposeViewController setSubject:#"HelloWorld"];
NSLog(#"%#",_pasteBoard.string);
[mailComposeViewController setMessageBody:_pasteBoard.string isHTML:NO];
[self presentViewController:mailComposeViewController animated:YES completion:nil];
}
}
-(void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
if(error) {
NSLog(#"%#",error.localizedDescription);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
Also, my view controller uses the correct protocol:
ViewController ()<MFMailComposeViewControllerDelegate>
I am running this on the simulator.
MFMailComposeViewController isn't supported in the simulator. You have to run on a device.

MFMailComposeViewController dismiss right after presented

It works fine on iPhone, this issue only occurred on iPad(iOS7). so weird.
I got this error log:
_serviceViewControllerReady:error: Error Domain=_UIViewServiceInterfaceErrorDomain Code=0 "The operation couldn’t be completed. (_UIViewServiceInterfaceErrorDomain error 0.)"
I did some search and some answers said that it may have something to do with customized UINavigationBar appearance.
In my case, I'm using a navigation controller and change its color and status bar color
in info.plist: set View controller-based status bar appearance as NO
then in appDelegate
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
I tried comment that line and the issue still exist. So I don't think that's the problem.
Here is my send email code and delegate
Class mailClass = (NSClassFromString(#"MFMailComposeViewController"));
if (mailClass != nil) {
if ([mailClass canSendMail]) {
MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
mailPicker.mailComposeDelegate = self;
NSString *emailbody = #"";
[mailPicker setSubject: subject];
NSArray *toRecipients = [NSArray arrayWithObject: recipient];
[mailPicker setToRecipients: toRecipients];
[mailPicker setMessageBody:emailbody isHTML:NO];
[self presentViewController:mailPicker animated:YES completion:nil];
}
}
- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[self dismissViewControllerAnimated:YES completion:nil];
}

Resources