iPadでUIActivityViewControllerがクラッシュする
iOSアプリでUIActivityViewController(OS標準の共有機能)を呼ぶ際に、iPadのみでクラッシュする現象に遭遇。
エラーメッセージは以下のとおり。
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の場合はsourceView
とsourceRect
を指定する必要がある。
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が表示される。