Difference between mailComposeDelegate and simple Delegate property

穿精又带淫゛_ 提交于 2019-12-13 02:08:51

问题


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.


回答1:


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:@"rajneesh071@gmail.com"];
        NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
        NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"];

        [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.




回答2:


Comment following line [mailer release];

I think it causing the problem




回答3:


check the headers - delegate property is of type id <UINavigationControllerDelegate> - because mail composer inherits from it, so, setting this to something might be a bad idea due to internal shenanigans of composer, to get notifications of mail composer state set self as composer's mailComposeDelegate.




回答4:


Add MFMailComposeViewControllerDelegate delegate in your .h file first & then check again.and also mailer.mailComposeDelegate = self; is right way not mailer.delegate=self also change it in your code and then check.

import MessageUI/MFMailComposeViewController.h this in .m with less than and greater than sign and also add MessageUI.framework.

NSArray *toRecipients = [NSArray arrayWithObject:@"vishal@ldh.01s.in"]; 
[mail setToRecipients:toRecipients];

add these two lines before set message body and then check.



来源:https://stackoverflow.com/questions/13491345/difference-between-mailcomposedelegate-and-simple-delegate-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!