LoginSignup
20
11

More than 5 years have passed since last update.

iPadでUIActivityViewControllerを呼ぶとクラッシュする現象の対応

Last updated at Posted at 2017-10-06

iPadでUIActivityViewControllerがクラッシュする

iOSアプリでUIActivityViewController(OS標準の共有機能)を呼ぶ際に、iPadのみでクラッシュする現象に遭遇。
2017-10-06 18.33.20.png
エラーメッセージは以下のとおり。

Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIActivityViewController (). In its current trait environment, the modalPresentationStyle of a UIActivityViewController with this style is UIModalPresentationPopover. You must provide location information for this popover through the view controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the view controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'

対処法

iPadの場合はsourceViewsourceRectを指定する必要がある。

UIActivityViewController *activityViewController = [[[UIActivityViewController alloc] initWithActivityItems:actItems applicationActivities:nil] autorelease];
activityViewController.popoverPresentationController.sourceView = self.view;
activityViewController.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width / 2.0, self.view.bounds.size.height / 2.0, 1.0, 1.0);

上記コードでクラッシュが回避され、ど真ん中にUIActivityViewControllerが表示される。

20
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
11