On iOS8 I'm using a UIActivityViewController to share a UIImage to Facebook/Twitter etc. It seemed to be working fine, but today it suddenly started crashing when running the code on my iPad. However, it still works as expected in the simulator.
My code:
UIActivityViewController *controller =
[[UIActivityViewController alloc]
initWithActivityItems:@[text, url, myImage]
applicationActivities:nil];
[self presentViewController:controller animated:YES completion:nil];
Upon crashing, Xcode spits out:
Discovered extensions: {( {id = com.apple.share.Facebook.post}, {id = com.apple.share.Twitter.post}, {id = com.apple.share.TencentWeibo.post}, {id = com.apple.share.SinaWeibo.post} )} for attributes: { NSExtensionActivationRule = { extensionItems = ( { attachments = ( { registeredTypeIdentifiers = ( "public.image" ); }, { registeredTypeIdentifiers = ( "public.plain-text" ); }, { registeredTypeIdentifiers = ( "public.url" ); } ); } ); }; NSExtensionPointName = ( "com.apple.share-services", "com.apple.ui-services", "com.apple.services" ); } 2014-08-07 21:38:59.208 collageTest[279:11021] LaunchServices: invalidationHandler called 2014-08-07 21:38:59.212 collageTest[279:11016] Discovered extensions: {( {id = com.apple.share.Flickr.post}, {id = com.apple.mobileslideshow.StreamShareService}, {id = com.apple.share.Twitter.post}, {id = com.apple.share.Facebook.post}, {id = com.apple.share.Vimeo.post}, {id = com.apple.share.SinaWeibo.post}, {id = com.apple.share.TencentWeibo.post} )} for attributes: { NSExtensionPointName = "com.apple.share-services"; } 2014-08-07 21:38:59.216 collageTest[279:11021] LaunchServices: invalidationHandler called
Looking at the docs, I needed to define a source view for the popover controller
UIActivityViewController *controller =
[[UIActivityViewController alloc]
initWithActivityItems:@[text,url,myImage]
applicationActivities:nil];
[self presentViewController:controller animated:YES completion:nil];
UIPopoverPresentationController *presentationController =
[controller popoverPresentationController];
presentationController.sourceView = self.view;
popoverPresentationController
was new to iOS 8 and will crash on iOS 7. It'll also be nil on iPhone because it's only in a UIPopover
on iPad. Here's Christian's answer in Swift, with those facts taken into account:
let controller = UIActivityViewController(activityItems: [text, url, myImage], applicationActivities: nil)
presentViewController(controller, animated: true, completion: nil)
if #available(iOS 8.0, *) {
let presentationController = controller.popoverPresentationController
presentationController?.sourceView = view
}
let controller = UIActivityViewController(activityItems: [text, url, myImage], applicationActivities: nil)
presentViewController(controller, animated: true, completion: nil)
if controller.respondsToSelector("popoverPresentationController") {
// iOS 8+
let presentationController = controller.popoverPresentationController
presentationController?.sourceView = view
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With