I am working on an app that automatically sends me an email message in certain circumstances but
I am unable to automatise the sending of the email, as the message composer picker comes up and I am required to physically press on the Send button...
Is it possible to automatise the "pressure" on the SEND button or does Apple prevent this to avoid spamming maybe?
What are the options for "completion"?
If this is not possible, is it then possible to send the email without using the message picker?
To bring up the message interface I'm using:
[self presentViewController:picker animated:YES completion:nil];
- (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];
}
It is not possible to send an email from the user's mail account without MFMailComposeViewController and user's explicit interaction.
Related
The MFMailComposeViewController is dismissing immediately when appear
- (IBAction)btnContactPressed:(id)sender
{
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"Feedback"];
NSArray *toRecipients = [NSArray arrayWithObjects:#"salimullah240#gmail.com", nil];
[mailer setToRecipients:toRecipients];
[self presentViewController:mailer animated:YES completion:nil];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"Mail cancelled: you cancelled the operation and no email message was queued.");
break;
case MFMailComposeResultSaved:
NSLog(#"Mail saved: you saved the email message in the drafts folder.");
break;
case MFMailComposeResultSent:
NSLog(#"Mail send: the email message is queued in the outbox. It is ready to send.");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail failed: the email message was not saved or queued, possibly due to an error.");
break;
default:
NSLog(#"Mail not sent.");
break;
}
// Remove the mail view
[self dismissViewControllerAnimated:YES completion:nil];
}
Your code is correct. I tried and worked fine. The MFMailComposeViewController component can't be tested in the iOS simulator only in a device.
If you look this Thread in Apple Developer Forums the problem has a ticket in Apple Bug Report but still without any fix.
Also, just make sure you are importing:
#import <MessageUI/MFMailComposeViewController.h>
and adding the delegate:
#interface ViewController () <MFMailComposeViewControllerDelegate>
I'm sending an email with very trivial method
#import <MessageUI/MFMailComposeViewController.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMessageComposeViewController.h>
-(void) sendEmailto: (NSArray*)p_recipient withSubject:(NSString*)p_subject body:(NSString*)p_body andAttachment:(NSData*)p_attachment
{
MFMailComposeViewController *emailComposer = [[MFMailComposeViewController alloc] init];
emailComposer.mailComposeDelegate = self;
if ([MFMailComposeViewController canSendMail] == YES)
{
[emailComposer setSubject:p_subject];
if(p_recipient != nil)
{
[emailComposer setToRecipients:p_recipient];
}
if (p_body!= nil && [p_body isEqualToString:#""]==NO)
{
[emailComposer setMessageBody:p_body isHTML:NO];
}
if(p_attachment != nil)
{
[emailComposer addAttachmentData:p_attachment mimeType:#"image/jpeg" fileName:#"image.jpg"];
}
// Present mail view controller on screen
[emailComposer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:emailComposer animated:YES];
self.sentEmailTargetController = p_target;
}
else
{
NSLog(#"Can't Open Email");
}
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(#"e-mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(#"e-mail saved");
break;
case MFMailComposeResultSent:
NSLog(#"e-mail sent");
break;
case MFMailComposeResultFailed:
NSLog(#"e-mail sent failure: %#", [error localizedDescription]);
break;
default:
break;
}
if (![[self presentedViewController] isBeingDismissed])
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
}
I have the declared MFMailComposeViewControllerDelegate delegate in the interface
running it on my device the email console opens, I can write a message, clicking on send closes the email window but doesn't actually send the email. The method - (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error logs email sent but the mail does not reach it's destination. tried several email accounts.
I can write a message, clicking on send closes the email window but doesn't actually send the email
This is in fact within the spec, as documented:
Using this interface does not guarantee immediate delivery of the corresponding email message
Lots of things can go wrong, lack of Internet connection being among the most obvious. And of course even if the email is sent, that is no guarantee that it will reach anyone; other things can go wrong down the line.
I have been working on app that capture images and video and then send it to mail via MFMailComposer. I have created zip file of content and size around 6MB. I want to show loading view when user click on send button and hide mail controller and when mail actually sent i want to show the message via alert. Is there any way to do it? Any help will be appreciated.
You can use the MFMailComposeViewControllerDelegate Methods to get info if the mail has been sent:
- (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];
}
Don't forget to add the MFMailComposeViewControllerDelegate to your .h file
Excuse me if im missing something I have only started this iOS dev yesterday. I have a basic application where a user can snap up to 3 pictures, these are held in 3 different UIImageViews, the app also retrieves the gps coordinates and changes these into an address.
What I want to do is be able to send these images and information to an email address.
Currently I have a send button which is linked to an action.
Thanks
- (IBAction)sendFinalItem:(UIButton *)sender {
NSLog(#"send button pressed");
MFMailComposeViewController *mailcontroller = [[MFMailComposeViewController alloc] init];
[mailcontroller setMailComposeDelegate:self];
NSString *email =#"MYEMAIL#MYEMAIL.CO.UK";
NSArray *emailArray = [[NSArray alloc] initWithObjects:email, nil];
[mailcontroller setToRecipients:emailArray];
[mailcontroller setSubject:#"[Urgent]Potential Job, iPhone snapped"];
[self presentViewController:mailcontroller animated:YES completion:nil];
}
- (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];
}
In your sendFinalItem method write this..
NSData *dataImage = UIImageJPEGRepresentation(yourImageViewName.image, 0.5f);
[mailer addAttachmentData:dataImage mimeType:#"image/jpeg" fileName:#"Image_1.jpg"]
Hope this helps.
EDIT:
[mailer setMessageBody:yourTextView.text isHTML:NO];
You can set isHTML to YES.(If your textview contains HTML data.)
EDIT:
If you want good quality image then use UIImagePNGRepresentation(yourImageViewName.image) instead of UIImageJPEGRepresentation(yourImageViewName.image, 0.5f)
Happy coding..
I use the SLComposeViewController to post a facebook message. But the SLComposeViewController result returns SLComposeViewControllerResultDone even when the facebook password is incorrect.
Here my code:
SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result){
[fbController dismissViewControllerAnimated:YES completion:nil];
switch(result){
case SLComposeViewControllerResultCancelled:
default:
{
CCLOG(#"Cancelled.....");
[self cancel];
}
break;
case SLComposeViewControllerResultDone:
{
[self success];
}
break;
}};
The message is not posted with an incorrect password.
How to check if the user is correctly logged or is the message is correctly posted?
Thanks.