2

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! ;)

1 Answer 1

0

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

2
  • Interesting! But I don't understand how the system determines which class is currently active on window. Why when a VC is in a tabbarcontroller it considers is not the active controller? Nov 5, 2013 at 9:59
  • You donot need to bother about that. It's the system which takes care of it. It keeps the track of view controllers through stack that which gets him the controller on the top. Nov 5, 2013 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.