0

In my iphone app. I'm Creating Iphone Mail Chat Sheet using MFMailComposeViewController. Now when I enter the second time,the mail sheet, my app is getting crashed. I searched in Google. But I couldn't find the solution. Anybody help me to solve my issue. PLease see this below code and help me where am I doing wrong.

if ([MFMailComposeViewController canSendMail])
        {
            controller = [[MFMailComposeViewController alloc] init];
            controller.mailComposeDelegate = self;
            [controller setSubject:@""];
            [controller setToRecipients:array1];
            [controller setMessageBody:@"" isHTML:NO];
            [controller setMailComposeDelegate: self];
            [controller setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
            [self.navigationController presentModalViewController:controller animated:NO];
            [controller release];
        }

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{
    if(result == MFMailComposeResultSent)
    {
        [[self parentViewController] dismissModalViewControllerAnimated:YES];
    }
    else if (result == MFMailComposeResultCancelled)
    {
        [[self parentViewController] dismissModalViewControllerAnimated:YES];
    }
 }
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
Gopinath
  • 5,392
  • 21
  • 64
  • 97

6 Answers6

0

I think this code makes the issue:

[[self parentViewController] dismissModalViewControllerAnimated:YES];

Instead of the above use:

[self dismissModalViewControllerAnimated:YES];
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • Thanks for your reply. sorry. I got the issue. Now when i enter the second time to the mail sheet i click the send button. my app getting crashing. What can i do. What is the Problem of this issue? – Gopinath Dec 11 '12 at 05:03
  • 2
    @Gopinath, can you give us the crash log? – Ilanchezhian Dec 11 '12 at 05:10
  • @Gopinath: please check the viewWillAppear and viewDidAppear methods of your navigation controller view class – Midhun MP Dec 11 '12 at 05:10
  • @Aadhira Thanks for reply. -[myViewContrl showErrorMessage:]: unrecognized selector sent to instance 0x883a400 – Gopinath Dec 11 '12 at 05:20
  • @Gopinath: then issue is with the showErrorMessage method call. Where are you calling that method ? Also check my previous comment – Midhun MP Dec 11 '12 at 05:28
  • @MidhunMP Ya. thanks for you reply. Ya. I have checked showErrorMessage this method am calling in the same class. And also i have viewWillAppear and viewDidAppear method. I want to add anything in inside of these methods. – Gopinath Dec 11 '12 at 05:43
  • @Gopinath: where are you calling that method ? I think that method is being called after dismissing the view. – Midhun MP Dec 11 '12 at 06:06
  • check my answer http://stackoverflow.com/questions/13491345/difference-between-mailcomposedelegate-and-simple-delegate-property/13491442#13491442 – Rajneesh071 Dec 11 '12 at 07:28
0

try to write this delegate method instead of you have writen the same method

and let me know whether it is working or not!!!!

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
switch (result)
{
    case MFMailComposeResultCancelled:
        NSLog(@"canceled");
        break;
    case MFMailComposeResultSaved:
        NSLog(@"saved");
        break;
    case MFMailComposeResultSent:
        NSLog(@"sent");
        [self showErrorMessage: @"Email sent successfully"];
        break;
    case MFMailComposeResultFailed:
        NSLog(@"failed");
        [self showErrorMessage: @"Failed to send email"];
        break;
    default:
        NSLog(@"not sent");
        break;
}
[self dismissViewControllerAnimated:YES completion:nil];

}

happy coding!!!!!!!

NiravPatel
  • 3,260
  • 2
  • 21
  • 31
  • thanks for your help. But when i enter secound time in mail sheet i clicked sent button. then my app getting crashing. But in my console i got this error. -[myviewcontroler showErrorMessage:]: unrecognized selector sent to instance 0x883a400 – Gopinath Dec 11 '12 at 05:21
0

Replace this code:

[self.navigationController presentModalViewController:controller animated:NO];

with:

[self presentViewController:controller animated:YES completion:nil];
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Rushabh
  • 3,208
  • 5
  • 28
  • 51
  • Thanks for reply.Sorry. i got this error in console. -[myViewCotroler showErrorMessage:]: unrecognized selector sent to instance 0x9839a00 – Gopinath Dec 11 '12 at 05:31
  • @Gopinath, remove that line which `[self showErrorMessage:` and check. – iDev Dec 11 '12 at 05:33
  • @ACB May i know the problem of this issue? – Gopinath Dec 11 '12 at 05:55
  • @Gopinath, You need to post your `showErrorMessage` and the line where you are calling this method. There is a serious mismatch between these two. That is the reason for crashing. – iDev Dec 11 '12 at 05:57
  • @ACB sorry for the delay. please wait. – Gopinath Dec 11 '12 at 06:05
  • @Gopinath, Check my answer and let me know. That should fix it. – iDev Dec 11 '12 at 06:20
  • @ACB sorry. i deleted that showErrorMessage line. And also i got crashing. what can i do?. – Gopinath Dec 11 '12 at 06:29
  • Can you post the crash message which is there now after removing that? What is the new crash? – iDev Dec 11 '12 at 06:32
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20917/discussion-between-gopinath-and-acb) – Gopinath Dec 11 '12 at 06:34
  • check my answer http://stackoverflow.com/questions/13491345/difference-between-mailcomposedelegate-and-simple-delegate-property/13491442#13491442 – Rajneesh071 Dec 11 '12 at 07:30
0

in method which you are opening the mail view ,replace your code with the below code.and tell me whether it is working or not!!!!!! happy coding!!!!

 MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];

if ([MFMailComposeViewController canSendMail] == NO) {
        return;
}
else
{
    picker.mailComposeDelegate = self;
    NSString *stringtitle=[NSString stringWithFormat:@"Apple"];
    [picker setSubject:stringtitle];

    NSData *imgDataLoop = UIImagePNGRepresentation(shaereImage.image);

    [picker addAttachmentData:imgDataLoop mimeType:@"image/png" fileName:@"rainy"];

    [self presentViewController:picker animated:YES completion:nil];
    [picker release];

}
NiravPatel
  • 3,260
  • 2
  • 21
  • 31
0

Change your methods to,

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"canceled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"sent");
            [self showErrorMessage: @"Email sent successfully"];
            break;
        case MFMailComposeResultFailed:
            NSLog(@"failed");
            [self showErrorMessage: @"Failed to send email"];
            break;
        default:
            NSLog(@"not sent");
            break;
    }
    [self dismissViewControllerAnimated:YES completion:nil];

}

-(void)showErrorMessage:(NSString *)iMessage
{
        NSLog(@"%@", iMessage);
}

As I mentioned earlier, your method signature was different while calling and in the definition part. It should be the same at both places as shown above.

While calling the method was [self showErrorMessage: @"Failed to send email"]; but in definition you were not accepting the string part. You need to have an input param for the same. You can also do the same by calling it as [self showErrorMessage]; and then keep the showErrorMessage method as it is.

iDev
  • 23,310
  • 7
  • 60
  • 85
0
-(void)mail
{ 
  if ([MFMailComposeViewController canSendMail])
    {

    MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
    mailController.mailComposeDelegate = self;

    [mailController setSubject:@"your Subject"];
    [mailController setMessageBody:@"your message" isHTML:NO];

    UIImage *tmp = [UIImage imageNamed:@"Icon.png"];
    NSData *myData = UIImageJPEGRepresentation(tmp, 1.0);

    [mailController addAttachmentData:myData mimeType:@"image/png" fileName:@"MyPhoto.png"];
    if (arrTo)
        [mailController setToRecipients:arrTo];

    [self presentModalViewController:mailController animated:YES];
    return YES;
}
else {
    return NO;
}
}
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{   

    [controller dismissModalViewControllerAnimated:YES];
}