Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing via UIActivityViewController to Twitter/Facebook etc. causing crash

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

like image 204
Christian J. B. Avatar asked Aug 07 '14 21:08

Christian J. B.


2 Answers

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;
like image 139
Christian J. B. Avatar answered Jan 01 '23 11:01

Christian J. B.


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:

Swift 2.0 (Xcode 7)

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
}

Swift 1.2 (Xcode 6)

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
}
like image 30
Andrew Avatar answered Jan 01 '23 12:01

Andrew