Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

UIActivityViewController Tutorial: Sharing Data

In this UIActivityViewController tutorial, you'll learn all about giving your users the ability to export their data, and share it with others.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1018-uiactivityviewcontroller-tutorial-sharing-data

Hi Andy,

  1. iPhone 6.
    Works OK.

  2. iPad Mini 2.
    Can open files sent from my iPhone.

However, when I try to share the app crashes.

Also crashes when I try to add a photo.

  1. Using the downloaded finished app from TW site.

This is the error I get when trying to share on iPad.

2016-08-13 17:25:50.817 BeerTracker[450:199013] *** Terminating app due to uncaught exception ‘NSGenericException’, reason: ‘UIPopoverPresentationController (<_UIAlertControllerActionSheetRegularPresentationController: 0x1667d060>) should have a non-nil sourceView or barButtonItem set before the presentation occurs.’
*** First throw call stack:
(0x2464791b 0x23de2e17 0x294b3485 0x28f13e93 0x28f11db3 0x28e6b4c9 0x28e777db 0x28bb5b1d 0x246096c9 0x246079cd 0x24607dff 0x24557229 0x24557015 0x25b47ac9 0x28c2b189 0x3fffc 0x241ff873)
libc++abi.dylib: terminating with uncaught exception of type NSException

And for the add photo button I get

2016-08-13 17:29:50.794 BeerTracker[454:200080] *** Terminating app due to uncaught exception ‘NSGenericException’, reason: ‘Your application has presented a UIAlertController (<UIAlertController: 0x15a85200>) of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller’s popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.’
*** First throw call stack:
(0x2464791b 0x23de2e17 0x294b3485 0x28f13e93 0x28f11db3 0x28e6b4c9 0x28e777db 0x28bb5b1d 0x246096c9 0x246079cd 0x24607dff 0x24557229 0x24557015 0x25b47ac9 0x28c2b189 0x43ffc 0x241ff873)
libc++abi.dylib: terminating with uncaught exception of type NSException

Fixed.

  1. Add the following to the ‘share’ function before presenting the view controller.

activityViewController.popoverPresentationController?.barButtonItem = (sender as! UIBarButtonItem)

  1. Add the following to the ‘pickPhoto’ function before presenting the view controller.

alertController.modalPresentationStyle = .Popover
alertController.popoverPresentationController!.sourceView = imageView
alertController.popoverPresentationController!.sourceRect = imageView.bounds

Hi oldfella,

Thanks for the fix to the photo addition. How did you figure this out? I am curious as to how others debug.

Thanks
kt

I’m getting a memory leak when I share a URL via ActivityViewController.

URL is to file in tmp directory. Memory leak is from object OS_dispatch_semaphore responsible frame FPlsFileProviderBookmark.

Thanks for this awesome tutorial. It worked great when I tried it out and it greatly helped me in releasing the newest version of my app (Shoptimizer). Users are now able to share their shopping lists and recipes. :slight_smile:

The share file extension saves file to “disk” on iOS device.

// 5
let saveFileURL = path.appendingPathComponent("/(name).btkr")
(contents as NSDictionary).write(to: saveFileURL, atomically: true)
return saveFileURL

How does this get cleaned up? Will the share extension automagically delete this source file after the “share” is done? Or will this storage just continue to grow with zombie-exports?

This may do it:

In BeerDetailViewController.swift, right after:

let activityViewController = UIActivityViewController(
activityItems: [“Check out this beer I liked using Beer Tracker.”, url],
applicationActivities: nil)

insert the following:

    activityViewController.completionWithItemsHandler = {
        (activity, success, items, error) in
        print("Activity: \(activity) Success: \(success) Items: \(items) Error: \(error)")
        do {
            try FileManager.default.removeItem(at: url)
            print("File Removed from Documents Folder")
        } catch {
            print("Failed to remove item from Documents Folder")
        }
    }

Great tutorial saved me tons of time! Question: the file share works great in email and airdrop, but not in iMessage. The file shows, but tapping it causes no action. Any ideas?

@macandyp Great tutorial man. It works fine transferring bears using Air drop. But in email, email arrives, shows the file in email, but does not open the Beer in Beer tracker App. More over, can’t post to facebook.
Can you help me in this regard?

Waiting anxiously for your reply

@mobiledevwaus in my case, Air drop is working fine. but in email, file is displaying in email but not opening the app

This tutorial is more than six months old so questions regarding it are no longer supported for the moment. We will update it as soon as possible. Thank you! :]

1 Like