alejandromp
alejandromp

Reputation: 939

Why UIActivityViewController doesn't dismiss MFMailComposeViewController when the presenter is a UITabbarController child?

I have a very simple case where a button shows an UIActivityViewController to share some content. The problem is that the mail composer is not dismissed when the user cancel or sends email.

NSArray *items = [NSArray arrayWithObjects:@"share",nil];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];

I tried to dismiss the modal view in the activityViewController.completionHandler but then the console says that another animation is in process and sometimes crash the app, so this isn't the solution. Besides other social networks work well (facebook, twitter) without doing anything.

After some tests I detected that this only occurs if the UIViewController that presents the UIActivityViewController is a child of a UITabBarController. If in my AppDelegate I create the app like this it doesn't work:

UIViewController > UINavigationController > UITabbarController > Window

but if I remove the tabbar all works fine!:

UIViewController > UINavigationController > Window 

A workaround that solves the problem is presenting from the rootviewcontroller.

[self.view.window.rootViewController presentViewController:activityViewController animated:YES completion:NULL];

but I want to know what's happening here.

Thanks! ;)

Upvotes: 2

Views: 964

Answers (1)

Kumar Aditya
Kumar Aditya

Reputation: 1097

I had the same issue and after figuring out I get to know that when we don't present mail composer from the class which is currently active on window , it will fail to take responses some time like it will not get even present on every call, same is the problem with dismisal. It is not that it is only the case with UITabbarController. It can happen to any architecture when you are presenting it from different controller which is not currently on window. That is why your workaround is working and its not wrong though.

I was presenting mail composer from a button tapped inside uipopover. Mail composer was being presented in that popover controller class. And I was facing the same issue you are facing. Then I changed my code to :

MFMailComposeViewController *mailComposer = [MFMailComposeViewController new];

    [mailComposer addAttachmentData:data mimeType:@"application/pdf" fileName:model.documentTitle];

    [mailComposer setSubject:model.documentTitle]; // Use the document file name for the subject

    if(kBccEmailID)
        [mailComposer setBccRecipients:[[NSArray alloc]initWithObjects:kBccEmailID, nil ]];

    mailComposer.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

    mailComposer.modalPresentationStyle = UIModalPresentationFormSheet;

    AppDelegate *delagate =(AppDelegate *) [[UIApplication sharedApplication]delegate];

    mailComposer.mailComposeDelegate = [[(UINavigationController *)delagate.window.rootViewController viewControllers] lastObject]; // Set the delegate


    [(UIViewController *)[[(UINavigationController *)delagate.window.rootViewController viewControllers] lastObject] presentViewController:mailComposer animated:YES completion:nil];

[(UIViewController *)[[(UINavigationController *)delagate.window.rootViewController viewControllers] lastObject] is an alternative for your 'self.view.window.rootViewController'.

Upvotes: 0

Related Questions