App crashing after touching email text MFMailComposeViewController - objective-c

I'm implementMFMailComposeViewController in my demp app. However, for some reasons, my app crashes after I touch to add texts in the email text field. But sending WITHOUT adding any text works fine.
My Xcode is not showing too much. Here is what I'm getting:
I'm setting an initial text already in the email text field. Maybe the issue is here? request any code and I'll be happy to include it.
UPDATE:
Here 2 methods where the first method openMail fires up when touching the UIButton
- (IBAction)openMail:(id)sender
{
if ([MFMailComposeViewController canSendMail])
{
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"Feedback from Demo App user"];
NSArray *toRecipients = [NSArray arrayWithObjects:#"myEMAIL#hotmail.com", #"myEMAIL2#gmail.com", nil];
[mailer setToRecipients:toRecipients];
NSString *emailBody = #"Happy to hear your feedback!";
[mailer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailer animated:YES];
[mailer release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failure"
message:#"Your device doesn't support the composer sheet"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
}
- (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 dismissModalViewControllerAnimated:YES];
}

Probably you're using ARC, and you're not have strong pointer to your MFMailComposeViewControllerand after display, ARC is releasing it.
use
#property (nonatomic,stron) MFMailComposeViewController *mail;
and when you're initialize MFMailComposeViewController:
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
self.mail = mailer;

Do you still have this line in your code:
[mailer release];
If so that is the problem. If not, then PLEASE update our code sample so its exactly what you are using now.

Related

MFMessageComposeViewController - Getting stuck after sending SMS in ios7

After sending SMS from my phone(it's working properly) in my project using the MFMessageComposeViewController I can't get back to the app or press cancel. Instead I'm stuck in Apple SMS interface and I can't do anything else then to restart the app. Do I miss something in didFinishWithResult or could it be something else? Thanks
- (IBAction)sendSMS:(id)sender {
MFMessageComposeViewController *controller =
[[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
NSString *str= #"Hi";
controller.body = str;
controller.recipients = [NSArray arrayWithObjects:
#"", nil];
controller.delegate = self;
[self dismissViewControllerAnimated:YES
completion:nil];
[self presentViewController:controller animated:YES completion:nil];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result
{
switch (result) {
case MessageComposeResultCancelled:
break;
case MessageComposeResultFailed:
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Failed to send SMS!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
break;
}
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}

MFMailComposer Dismissing immediately after launch

I am trying to launch the MFMailComposer on the iOS 7 simulator and as soon as it comes up it immediately dissmises itself and I get the following error in the debugger.
_serviceViewControllerReady:error: Error Domain=NSCocoaErrorDomain Code=4097 "The operation couldn’t be completed. (Cocoa error 4097.)"
Here is my code
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0:
break;
case 1:{
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:[NSString stringWithFormat:#"%# Totals Report",_teamStats.relationshipTeam.teamName]];
[mailViewController setMessageBody:#"\n\n\n\n\nSent From HoopMetrics" isHTML:NO];
// Attach a doc to the email
NSData* data = [_teamStats.relationshipTeam pdfDataFromString:_teamStats.teamTotalsAsString];
[mailViewController addAttachmentData:data mimeType:#"application/pdf" fileName:#"Totals Report"];
[self presentViewController:mailViewController animated:YES completion:nil];
}
else{
HMAlertView*alert = [[HMAlertView alloc]initWithTitle:#"No Email" message:#"Please, set up an email account on your device" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
break;
}
I faced the same problem. Just turn off appearance customization for UINavigationBar or UIBarButtonItem or some another element that MFMailComposeViewController may use.
You can get around this with the following hack using performSelector:
- (IBAction)sendEmail:(id)sender {
[self performSelector:#selector(showEmailComposer) withObject:nil afterDelay:0.0];
}
-(void) showEmailComposer{
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
//Set recipients...other stuff
[mailViewController setToRecipients:recipients];
[mailViewController setSubject:subject];
[mailViewController setMessageBody:body isHTML:isHTML];
mailViewController.title = #"Email VC Title";
mailViewController.mailComposeDelegate = delegate;
[self presentViewController:mailViewController
animated:YES
completion:NULL];
}

MFMailComposeViewController won't dismiss on iOS 6

In iOS 6 the presented MFMailComposeViewController will not dismiss if user attempts to send second email...
Everything works perfectly the first go around and email is sent. However, if email option is selected again the MFMailComposeViewController will not dismiss on cancel.
Here is how I implemented it:
- (IBAction)buttonEmailClick:(id)sender {
if (![MFMailComposeViewController canSendMail]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Can't send" message:#"This device is unable to send emails." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
return;
}
NSDictionary *contactsInfo = [self contactsInfoFromPlistNamed:kConfigPlistName];
[mailComposeViewController setToRecipients:[NSArray arrayWithObject:[contactsInfo objectForKey:#"email"]]];
//[mailComposeViewController setSubject:kEmailDefaultSubject];
//[mailComposeViewController setMessageBody:text isHTML:NO];
[self presentModalViewController:mailComposeViewController animated:YES];
}
and then this:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
UIAlertView *alert = nil;
if (result == MFMailComposeResultSent) {
alert = [[UIAlertView alloc] initWithTitle:#"Sent" message:#"Your email was sent." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
}
else if (result == MFMailComposeResultFailed) {
alert = [[UIAlertView alloc] initWithTitle:#"Failed" message:#"An error occured and your email was not sent." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
}
[alert show];
[self dismissModalViewControllerAnimated:YES];
}
It works fine in iOS 5 but not in iOS 6. I have tried replacing with non deprecated methods for iOS 6, but it doesn't work.
Have you tried creating a fresh MFMailComposeViewController each time they go to send an email? I'm not sure if you should be reusing it.
You can try this:
MFMailComposeViewController * composer = [[MFMailComposeViewController alloc] init];
composer.delegate = self;
-(void)mailComposeController:didFinishWithResult:error: should be called if you assign that class to the delegate

Undeclared identifier 'mailComposeController'

Here is my .m code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *emailCell = [tableView cellForRowAtIndexPath:indexPath];
if ([emailCell.textLabel.text isEqualToString:#"Contact Developer"]) {
NSLog(#"Email button pressed...");
if([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"A Message from MobileTuts+"];
NSArray *toRecipients = [NSArray arrayWithObjects:#"fisrtMail#example.com", #"secondMail#example.com", nil];
[mailer setToRecipients:toRecipients];
NSString *emailBody = #"Have you seen the MobileTuts+ web site?";
[mailer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailer animated:YES];
[mailer release];
- (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 dismissModalViewControllerAnimated:YES];
}
}
else {
UIAlertView *emailAlert = [[UIAlertView alloc] initWithTitle:#"Error!"
message:#"Please make sure you have an email address configured in your Mail app."
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[emailAlert show];
[emailAlert release];
}
}
}
I have imported everything correctly in .h and .m, but the email won't close... I have tried as suggested on Stack Overflow to fiddle with the mailer.mailComposeDelegate = self; but I am still getting an undeclared identifier error. How do I fix this? Thanks.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
Move this function out of the tableview delegate method. You should learn some basics. You cannot have a method within another method. If you need then you can call the method by [self callingFunctionName];
In you case,
if ([emailCell.textLabel.text isEqualToString:#"Contact Developer"]) {
NSLog(#"Email button pressed...");
[self sendEmail];
}
-(void)sendEmail{
if([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"A Message from MobileTuts+"];
NSArray *toRecipients = [NSArray arrayWithObjects:#"fisrtMail#example.com", #"secondMail#example.com", nil];
[mailer setToRecipients:toRecipients];
NSString *emailBody = #"Have you seen the MobileTuts+ web site?";
[mailer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailer animated:YES];
[mailer release];
}
}
This will open the mail composer. When you send, cancel or save the mail, the delegate function
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
will automatically be called.

Not receiving email sent using MessageUI framework

I am using the MessageUI framework to send an email but I never receive that email when sent.
I am importing #import <MessageUI/MessageUI.h>
and then I have the following code
- (void)emailFile
{
if(![MFMailComposeViewController canSendMail]) {
UIAlertView *cantSend = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Device not configured to send email" delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[cantSend show];
[cantSend release];
} else {
MFMailComposeViewController *mailView = [[[MFMailComposeViewController alloc] init] autorelease];
mailView.mailComposeDelegate = self;
[mailView setToRecipients:[NSArray arrayWithObject:#"matthew.inman#cdl.co.uk"]];
[mailView setSubject:#"Test"];
[mailView setMessageBody:#"This is a text message" isHTML:NO];
[self presentModalViewController:mailView animated:YES];
}
}
and
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
if(error) {
UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:#"Mail Error" message:[error localizedDescription] delegate:self cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[errorView show];
[errorView release];
} else {
switch (result) {
case MFMailComposeResultSent:
NSLog(#"Sent Mail");
break;
case MFMailComposeResultSaved:
NSLog(#"Mail Saved");
break;
case MFMailComposeResultCancelled:
NSLog(#"Mail Cancelled");
break;
case MFMailComposeResultFailed:
NSLog(#"Mail Failed");
break;
default:
break;
}
}
[controller dismissModalViewControllerAnimated:YES];
}
I get the message "Sent Mail" in the console but I like I said I never receive the email that I am sending.
I have gone through the apple documentation and can't find anything that helps can anyone else help me please. I'm I doing something wrong?
Make sure you are testing on a device, email will not be sent via the simulator.