Skip to content
This repository has been archived by the owner on Jun 2, 2018. It is now read-only.

MFMailComposeViewController overrides default delegate #24

Closed
meoz445 opened this issue Sep 4, 2011 · 6 comments
Closed

MFMailComposeViewController overrides default delegate #24

meoz445 opened this issue Sep 4, 2011 · 6 comments

Comments

@meoz445
Copy link

meoz445 commented Sep 4, 2011

Can't use default MFMailComposeViewController anymore, when dismissing viewController delegate method is not called.

I think the problem is that the default setMailComposeDelegate is overridden even when using the default MFMailComposeViewController.

MFMailComposeViewController+BlocksKit.m

[self swizzleSelector:@selector(mailComposeDelegate) withSelector:@selector(bk_mailComposeDelegate)];
[self swizzleSelector:@selector(setMailComposeDelegate:) withSelector:@selector(bk_setMailComposeDelegate:)];
@zwaldowski
Copy link
Collaborator

Yes, that's the idea. BlocksKit overrides the default delegate with a block trampoline, but also holds on to the delegate you set and calls both your callback and the block.

I just tested this again and it works for me:

- (IBAction)sendTestMail:(id)sender {
    MFMailComposeViewController *vc = [[MFMailComposeViewController alloc] init];
    vc.mailComposeDelegate = self;
    vc.subject = @"Test";
    vc.completionHandler = ^(MFMailComposeResult result, NSError *error){
        NSLog(@"Block result:  %i", result);
    };
    [self presentModalViewController:vc animated:YES];
}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    NSLog(@"Delegate result:  %i", result);
}

Please be sure that you are calling setMailComposeDelegate: properly.

@meoz445
Copy link
Author

meoz445 commented Sep 4, 2011

This is my sample code, in certain cases I don't need to set the blocks delegate.

If this works for you I need to investigate further why my delegate is not called when adding the Blockskit to my project

-(void)lauchEmailController
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"subject"];
[picker setMessageBody:@"email body" isHTML:NO];
[self.parentViewController presentModalViewController:picker animated:YES];
[picker release];
}

//this delegate callback never gets called when having BlockKit added as a reference into the project

  • (void)mailComposeController:(MFMailComposeViewController_)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError_)error
    {
    [controller dismissModalViewControllerAnimated:YES];
    }

@zwaldowski
Copy link
Collaborator

Your delegate callback is being called. -dismissModalViewControllerAnimated: does NOT work on the view controller itself, it works on the view controller that is presenting the mail view controller. This behavior has partially changed in iOS 5. To dismiss a view controller, use this:

#ifdef __IPHONE_5_0 // preemptive compatibility with the 5.0 SDK
if ([self respondsToSelector:@selector(presentingViewController)]) 
    [[self presentingViewController] dismissModalViewControllerAnimated:YES]; // iOS 5
else
#endif
    [self.parentViewController dismissModalViewControllerAnimated:YES]; // 4.3 and below

Issue closed.

@sbrocket
Copy link

For future reference, the comment about -dismissModalViewControllerAnimated: not working on the view controller itself is incorrect.

From the documentation
The parent view controller is responsible for dismissing the modal view controller it presented using the presentModalViewController:animated: method. If you call this method on the modal view controller itself, however, the modal view controller automatically forwards the message to its parent view controller.

@zwaldowski
Copy link
Collaborator

L

Sent from my iPhone

On Oct 10, 2011, at 1:39 PM, Bryan Henry
reply@reply.github.com
wrote:

For future reference, the comment about -dismissModalViewControllerAnimated: not working on the view controller itself is incorrect.

From the documentation
The parent view controller is responsible for dismissing the modal view controller it presented using the presentModalViewController:animated: method. If you call this method on the modal view controller itself, however, the modal view controller automatically forwards the message to its parent view controller.

Reply to this email directly or view it on GitHub:
#24 (comment)

@zwaldowski
Copy link
Collaborator

That would've made things a lot easier a long time ago, thanks! I
didn't know it automatically forwarded.

On Oct 10, 2011, at 1:39 PM, Bryan Henry
reply@reply.github.com
wrote:

For future reference, the comment about -dismissModalViewControllerAnimated: not working on the view controller itself is incorrect.

From the documentation
The parent view controller is responsible for dismissing the modal view controller it presented using the presentModalViewController:animated: method. If you call this method on the modal view controller itself, however, the modal view controller automatically forwards the message to its parent view controller.

Reply to this email directly or view it on GitHub:
#24 (comment)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants