Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIActivityViewController crashing on iOS 8 iPads

I am currently testing my app with Xcode 6 (Beta 6). UIActivityViewController works fine with iPhone devices and simulators but crashes with iPad simulators and devices (iOS 8) with following logs

Terminating app due to uncaught exception 'NSGenericException',  reason: 'UIPopoverPresentationController  (<_UIAlertControllerActionSheetRegularPresentationController: 0x7fc7a874bd90>)  should have a non-nil sourceView or barButtonItem set before the presentation occurs. 

I am using following code for iPhone and iPad for both iOS 7 as well as iOS 8

NSData *myData = [NSData dataWithContentsOfFile:_filename]; NSArray *activityItems = [NSArray arrayWithObjects:myData, nil]; UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:nil applicationActivities:nil]; activityViewController.excludedActivityTypes = @[UIActivityTypeCopyToPasteboard]; [self presentViewController:activityViewController animated:YES completion:nil]; 

I am getting a similar crash in of one my other app as well. Can you please guide me ? has anything changed with UIActivityViewController in iOS 8? I checked but i did not find anything on this

like image 363
Bhumit Mehta Avatar asked Sep 03 '14 12:09

Bhumit Mehta


1 Answers

On iPad the activity view controller will be displayed as a popover using the new UIPopoverPresentationController, it requires that you specify an anchor point for the presentation of the popover using one of the three following properties:

  • barButtonItem
  • sourceView
  • sourceRect

In order to specify the anchor point you will need to obtain a reference to the UIActivityController's UIPopoverPresentationController and set one of the properties as follows:

if ( [activityViewController respondsToSelector:@selector(popoverPresentationController)] ) {  // iOS8  activityViewController.popoverPresentationController.sourceView = parentView;  } 
like image 124
mmccomb Avatar answered Sep 19 '22 12:09

mmccomb