I am having a trouble using MFMailComposeViewController.
When I click on a button, an instance of MFMailComposeViewController should present itself:
http://postimg.org/image/i24gn4oi7/
But when it does, this happens:
http://postimg.org/image/kdwm0isrl/
Here's the code:
- (IBAction)actionEmailComposer {
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
NSMutableArray *email = [[NSMutableArray alloc] initWithObjects:#"factto#gmail.com", nil];
[mc setToRecipients:email];
//[mc setSubject:#"SUBJECT_HERE"];
[mc setMessageBody:#"ReferĂȘncia: \nCor:" isHTML:NO];
mc.modalPresentationStyle = UIModalPresentationFullScreen;
// 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");
UIAlertView *msg = [[UIAlertView alloc] initWithTitle:#"Obrigado!" message:#"Logo logo entraremos em contato!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[msg show];
}
break;
case MFMailComposeResultFailed:
NSLog(#"Mail sent failure: %#", [error localizedDescription]);
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
Actually, on the iPad it does work properly, it just happens on the iPhone!
What is going on? What am I doing wrong?
Thanks in advance.
The view controller that you present the mail view controller from needs to be full screen and at the front, if it isn't some parts of the view won't respond properly to touches and / or be properly visible (as you see).
Related
Does anyone have a decent tutorial on how to implement mail composer for ios 5 either programmatically or with segues? Most of the tutorials that I found online are from old iOS versions. Thanks!
You could do something like this:
if([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
[mailController setMailComposeDelegate:self];
[mailController setSubject:#"Mail Subject!"];
[mailController setMessageBody:#"Here is your message body" isHTML:NO];
[mailController setToRecipients:[NSArray arrayWithObject:#"yourrecipent#domain.com"]];
NSData *imageData = UIImageJPEGRepresentation(imageToUpload, 1.0f);
if(imageData.length)
{
[mailController addAttachmentData:imageData mimeType:#"image/jpeg" fileName:#"Your_Photo.jpg"];
[self presentModalViewController:mailController animated:YES];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Invalid Image" message:#"The image couldn't be converted." delegate:nil cancelButtonTitle:nil otherButtonTitles:#"Okay", nil];
[alert show];
}
}
else NSLog(#"Hah. No mail for you.");
At first you have to add "MFMailComposeViewControllerDelegate" to interface section.
Also you have to add procedure to get response after user taps "Send button"
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result) {
case MFMailComposeResultSent:
NSLog(#"You sent the email.");
break;
case MFMailComposeResultSaved:
NSLog(#"You saved a draft of this email");
break;
case MFMailComposeResultCancelled:
NSLog(#"You cancelled sending this email.");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail failed: An error occurred when trying to compose this email");
break;
default:
NSLog(#"An error occurred when trying to compose this email");
break;
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
I am trying to sync Events added by my code in calendar application to available mail accounts. I just wanted to confirm that, is it possible in ios5?? I am trying to search for it but not able to find good solution for this. Any one having idea about this??
And I have one more question to ask that is, I am using ios5 new function which create new calendar in iphone's calendar application. Whenever I creates calendar of type local, I am able to create it. But I cant see that calendar in calendar list. Because of some reason it gets hide. Any idea about this??
Thank you in advance
First add MessageUI framework in your poject and import these two in your file
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
Then make a button to refer mail in xid and link it with a IBAction (like below):
-(IBAction)emailSettings:(id)sender{
Class mailClass = (NSClassFromString(#"MFMailComposeViewController"));
if (mailClass != nil)
{
// check whether the current device is configured for sending emails
if ([mailClass canSendMail])
{
[self displayComposerSheet];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"EMAIL" message:#"No Settings For Email" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
}
-(void)displayComposerSheet
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"StampedeBreakfast!"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:#"first#example.com"];
NSArray *ccRecipients = [NSArray arrayWithObjects:#"second#example.com", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:#"third#example.com"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
// Fill out the email body text
NSString *emailBody = [NSString stringWithFormat:#"Hey! This is my Email Body"];
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog( #"Result: canceled");
break;
case MFMailComposeResultSaved:
NSLog(#"Result: saved");
break;
case MFMailComposeResultSent:
NSLog(#"Result: sent");
break;
case MFMailComposeResultFailed:
NSLog( #"Result: failed");
break;
default:
NSLog(#"Result: not sent");
break;
}
[self dismissModalViewControllerAnimated:YES];
}
hope this will help you!!
I used the sample code on the Apple Dev site to learn how to set pre-composed emails, but is there a way to set precomposed SMS messages, similarly?
First you have to add the framework MessageUI to your project and import the library "MessageUI/MessageUI.h". Then conform to the protocol <MFMessageComposeViewControllerDelegate>.
Now to send an SMS:
- (IBAction) sendSMS:(id)sender
{
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
controller.body = #"The body of the SMS you want";
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
[controller release];
}
To catch the result of the sending operation:
- (void) messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch(result)
{
case MessageComposeResultCancelled: break; //handle cancelled event
case MessageComposeResultFailed: break; //handle failed event
case MessageComposeResultSent: break; //handle sent event
}
[self dismissModalViewControllerAnimated:YES];
}
The body property on MFMessageComposeViewController allows you to set the message body just like you can for an email.
Check out the documentation: http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMessageComposeViewController_class/Reference/Reference.html
See this article on Apple's Dev Center:
Sending an SMS Message
PresentModalViewController is now deprecated in IOS 6. So i used
[self presentViewController:controller animated:YES completion:nil];
whole code is as following
-(IBAction)sendSMSButtonTouchupInside:(id)sender
{
MFMessageComposeViewController *controller =
[[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
controller.body = #"Whatever you want";
controller.recipients = [NSArray arrayWithObjects:#"03136602888", nil];
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"MyApp" message:#"Unknown Error"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
switch (result)
{
case MessageComposeResultCancelled:
NSLog(#"Cancelled");
[alert show];
break;
case MessageComposeResultFailed:
[alert show];
break;
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
I have a very simple implementation of MFMailComposeViewController. It works very well - I pass a string from my app which will be e-mailed and then touch 'send' and the e-mail will send. No problems.
However, if I touch the 'cancel' button, the app will crash (EXC_BAD_ACCESS). Do I have to implement a special method for the cancellation of a MFMailComposeViewController? The send button will automatically do it the right way, but the cancel button won't. What is the difference between the two (except that in one case the e-mail will be sent and in the other it won't)?
Here is my code:
#pragma mark - EMail
-(IBAction)emailCurrentPage:(id)sender {
NSString *textToBeSend = #"Test";
MFMailComposeViewController *mailComposer;
mailComposer=[[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate=self;
[mailComposer setMessageBody:textToBeSend isHTML:NO];
[self presentModalViewController:mailComposer animated:YES];
[mailComposer release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
if (error) {
NSString *msgOFF = [[NSString alloc] initWithFormat:#"I could not send the e-mail for the following reason: %#", error];
UIAlertView *alertOFF = [[UIAlertView alloc]
initWithTitle:#"Error"
message:msgOFF
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertOFF show];
[alertOFF release];
[msgOFF release];
}
[self dismissModalViewControllerAnimated:NO];
}
Try this :
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
NSString *message = #"";
// Notifies users about errors associated with the interface
switch (result) {
case MFMailComposeResultCancelled:
message = #"Mail: canceled";
break;
case MFMailComposeResultSaved:
message = #"Mail: saved";
break;
case MFMailComposeResultSent:
{
message = #"Mail: sent";
//Your code
}
break;
case MFMailComposeResultFailed:
message = #"Mail: failed";
break;
default:
message = #"Mail: not sent";
break;
}
[self dismissModalViewControllerAnimated:YES];
}
I am trying to add email functionality to my app. I can get the MFMailComposeViewController to display correctly and pre-populate its subject and body, but for some reason when the user clicks on the "Cancel" or "Send" buttons in the nav bar the app just hangs. I inserted a NSLog() statement into the first line of mailComposeController"didFinishWithResult:error and it doesn't even print that line out to the console.
Does anybody have an idea what would cause the MFMailComposeViewController to hang like that?
Here is my code from the header:
#import "ManagedObjectEditor.h"
#import <MessageUI/MessageUI.h>
#interface MyManagedObjectEditor : ManagedObjectEditor
<MFMailComposeViewControllerDelegate, UIImagePickerControllerDelegate,
UINavigationControllerDelegate> {
}
- (IBAction)emailObject;
#end
from the implementation file:
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.delegate = self;
[mailComposer setSubject:NSLocalizedString(#"An email from me",
#"An email from me")];
[mailComposer setMessageBody:emailString
isHTML:YES];
[self presentModalViewController:mailComposer animated:YES];
[mailComposer release];
}
[error release];
[emailString release];
and here is the code from the callback:
#pragma mark -
#pragma mark Mail Compose Delegate Methods
- (void)mailComposeController:(MFMailComposeViewController *)controller
didFinishWithResult:(MFMailComposeResult)result
error:(NSError *)error {
NSLog(#"in didFinishWithResult:");
switch (result) {
case MFMailComposeResultCancelled:
NSLog(#"cancelled");
break;
case MFMailComposeResultSaved:
NSLog(#"saved");
break;
case MFMailComposeResultSent:
NSLog(#"sent");
break;
case MFMailComposeResultFailed: {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Error sending email!",#"Error sending email!")
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:NSLocalizedString(#"Bummer",#"Bummer")
otherButtonTitles:nil];
[alert show];
[alert release];
break;
}
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}
Thanks!
I got bit by this too, you need to set the mailComposeDelegate, not the delegate.