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.
Related
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;
}
}
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.
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.
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];
}
I am new to xcode, and am wondering how to send email in an app! My code is below, but I keep getting the error "No visible #interface for 'jakem' declares the selector 'presentViewControllerAnimated:'". Is my code completely wrong? Or did I just forget to declare the selector, and how do I declare the selector? I have researched all over the internet for at least an hour, and nothing is working. Someone please help me!
-(IBAction)sendEmail{
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
[composer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[composer setToRecipients:[NSArray arrayWithObjects:#"FrankMurphy.CEO#RomansXIII.com", nil]];
[composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:composer animated:YES];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
if(error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"error" message:[NSString stringWithFormat:#"error %#", [error description]] delegate:nil cancelButtonTitle:#"dismiss" otherButtonTitles:nil, nil];
[alert show];
[self dismissViewControllerAnimated:YES];
}
else {
[self dismissViewControllerAnimated:YES];
}
}
in .h header file....
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#interface SimpleEmailViewController : UIViewController <MFMailComposeViewControllerDelegate> // Add the delegate
- (IBAction)showEmail:(id)sender;
#end
in .m implementation file.....
- (IBAction)showEmail:(id)sender {
// Email Subject
NSString *emailTitle = #"Test Email";
// Email Content
NSString *messageBody = #"iOS programming is so fun!";
// To address
NSArray *toRecipents = [NSArray arrayWithObject:#"info#finetechnosoft.in"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
// 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");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail sent failure: %#", [error localizedDescription]);
break;
default:
break;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}
Check if you are MFMailComposeViewControllerDelegate.
You do this like
#interface YouClassName : UIViewController <MFMailComposeViewControllerDelegate>
#end
I think you're using the wrong method. Try
[self presentViewController:(UIViewController *) animated:(BOOL) completion:(void)completion];
instead of:
[self presentViewController:composer animated:YES];
I work for Sendgrid. We have an Objective-c library that lets you quickly send email from inside your app, https://github.com/sendgrid/sendgrid-objc. You can use cocoapods to quickly install the library in your project.
Then sending the email from your (IBAction) would look like this:
-(IBAction)sendEmail{
sendgrid *msg = [sendgrid user:#"username" andPass:#"password"];
msg.to = #"FrankMurphy.CEO#RomansXIII.com";
msg.from = #"me#bar.com";
msg.text = #"hello world";
msg.html = #"<h1>hello world!</h1>";
[msg sendWithWeb];
}