0

I want to attach pdf to the mail but I am getting following error when i run in iOS 8. when i open activity

Presenting view controllers on detached view controllers is discouraged.

When i click on mail, i am getting

Warning: Attempt to present <MFMailComposeViewController:> on <> whose view is not in the window hierarchy!

I am using the following code to do so,

activityVC = [[UIActivityViewController alloc]initWithActivityItems:PDFDataArray applicationActivities:nil];  
_popup = [[UIPopoverController alloc] initWithContentViewController:activityVC];
[_popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/4, 0, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

Not displaying a window.

VJVJ
  • 435
  • 3
  • 21

1 Answers1

0

This is happening because your UIPopoverController is not yet presented and still not a part of the window hierarchy.

The correct way to presnt UIActivityViewController is as follows:

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:PDFDataArray applicationActivities:nil];

if ([activityVC respondsToSelector:@selector(popoverPresentationController)] ) { 
    activityVC.popoverPresentationController.sourceView = myButton;
}    

[self presentViewController:activityVC animated:YES completion:nil];

No need of a UIPopoverController

Nishant
  • 12,529
  • 9
  • 59
  • 94
  • I tried, but i am getting error as "Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (<_UIAlertControllerActionSheetRegularPresentationController: 0x7b823de0>) should have a non-nil sourceView or barButtonItem set before the presentation occurs.'" – VJVJ Sep 09 '15 at 11:48
  • I need to use popovercontroller for iPad. Please check http://stackoverflow.com/questions/25644054/uiactivityviewcontroller-crashing-on-ios8-ipads – VJVJ Sep 09 '15 at 11:57
  • what is myButton i am not getting – VJVJ Sep 09 '15 at 12:11
  • myButton is any `UIView`,`UIButton`,`UIBarButtonItem` etc. object from where you want to show your popover for `UIActivityViewController` – Nishant Sep 09 '15 at 12:13
  • @elavarasan: Let me know if it worked for u. Please do accept the answer. – Nishant Sep 09 '15 at 12:37
  • I am getting error as UIPopoverPresentationController (<_UIAlertControllerActionSheetRegularPresentationController:>) should have a non-nil sourceView or barButtonItem set before the presentation occurs – VJVJ Sep 10 '15 at 09:44
  • Can you please add your complete code here. Unable to understand your problem. – Nishant Sep 10 '15 at 09:47
  • 1
    I dismiss view before attachment ([self dismissPopupViewControllerWithanimation:nil];) thats why i was getting "Warning: Attempt to present on <> whose view is not in the window hierarchy!" Now i am dismissing after sending an email. Now it is working fine. Thanks for your help – VJVJ Sep 10 '15 at 09:54