Search code examples
iphoneiosxcodeios5mfmailcomposeviewcontroller

Difference between mailComposeDelegate and simple Delegate property


Hi i am getting confuse on MFMailComposeViewController delegate property, when i set mailer.mailComposeDelegate app crash just after call [self presentModalViewController:mailer animated:YES]; and when i do mailer.delegate then app don't crash but its view can't hide after sending mail or just cancel it from its navigation bat button "Cancel". I am getting stuck why this happen. Let me share code, you get hint where i am doing mistake.

if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
    if(mailer)
    {
        mailer.mailComposeDelegate = self;
        //mailer.delegate=self;
        [mailer setSubject:@"What the Buck?"];
        imageData = UIImagePNGRepresentation(screenImgSubCat);        

        [mailer addAttachmentData:imageData mimeType:@"image/png" fileName:@"testapp"];        
        NSString *emailBody = @"What the Buck?! – www.testapp.com";
        [mailer setMessageBody:emailBody isHTML:NO];
        [self presentModalViewController:mailer animated:YES];
        //[mailer release];

    }
}
}

Updated

I change code and use mailer.mailComposeDelegate = self; and also comment this line [mailer release]; still giving me crash on when image is being loading. Here is the Image what i am getting after crash. enter image description here


Solution

  • In .h file are you adding MFMailComposeViewControllerDelegate

    @interface VideoPlayAndSharing : UIViewController
    <MFMailComposeViewControllerDelegate>  
    

    Display ComposerSheet

    -(void)displayComposerSheet
    {
        if ((videodta.length/1024)/1024 < 25)
        {
            NSLog(@"Video size >> %d",videodta.length/1024);
            MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
            picker.mailComposeDelegate = self;
    
            [picker setSubject:@"Your subject"];
    
    
            // Set up recipients
            NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"];
            NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];
            NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]"];
    
            [picker setToRecipients:toRecipients];
            [picker setCcRecipients:ccRecipients];
            [picker setBccRecipients:bccRecipients];
    
            [picker addAttachmentData:videodta mimeType:@"video/mp4" fileName:@"MyPersonalMessage"];
    
            // Fill out the email body text
            NSString *emailBody = @"Type your message here";
            [picker setMessageBody:emailBody isHTML:NO];
            [self presentModalViewController:picker animated:YES];
        }
        else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My Personal Message"
                                                            message:@"Video exceed the limit of 25 MB"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil, nil];
            [alert show];
        }
    }
    

    and delegate method

    - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
    {   
        // Notifies users about errors associated with the interface
        switch (result)
        {
            case MFMailComposeResultCancelled:
                message.text = @"Result: canceled";
                break;
            case MFMailComposeResultSaved:
                message.text = @"Result: saved";
                break;
            case MFMailComposeResultSent:
                message.text = @"Result: sent";
                break;
            case MFMailComposeResultFailed:
                message.text = @"Result: failed";
                break;
            default:
                message.text = @"Result: not sent";
                break;
        }
        [self dismissViewControllerAnimated:YES completion:nil];
    }  
    

    EDIT

    picker.mailComposeDelegate its delegate of MFMailComposeViewControllerDelegate

    Its respond to - (void)mailComposeController

    picker.delegate its delegate of UINavigationControllerDelegate

    Its respond to navigation controller not - (void)mailComposeController , so on cancel click it will not call, thats why your MFMailComposeViewController view is not hiding.