# UIActivityViewController Tutorial: Sharing Data

**URL:** <https://forums.kodeco.com/t/uiactivityviewcontroller-tutorial-sharing-data/18261>\
**Category:** 🎓 Courses & Articles\
**Created:** [August 8, 2016, 8:01pm UTC](https://forums.kodeco.com/t/uiactivityviewcontroller-tutorial-sharing-data/18261 "2016-08-08T20:01:36Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![macandyp](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/macandyp/32/108930_2.png) [@macandyp](https://forums.kodeco.com/u/macandyp)\
**Post date:** [August 8, 2016, 8:01pm UTC](https://forums.kodeco.com/t/uiactivityviewcontroller-tutorial-sharing-data/18261/1 "2016-08-08T20:01:36Z")

</div>

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](https://www.raywenderlich.com/1018-uiactivityviewcontroller-tutorial-sharing-data)

---

<div class="post-metadata">

**Author:** ![oldfella](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@oldfella](https://forums.kodeco.com/u/oldfella)\
**Post date:** [August 13, 2016, 12:37am UTC](https://forums.kodeco.com/t/uiactivityviewcontroller-tutorial-sharing-data/18261/2 "2016-08-13T00:37:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![oldfella](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@oldfella](https://forums.kodeco.com/u/oldfella)\
**Post date:** [August 13, 2016, 7:30am UTC](https://forums.kodeco.com/t/uiactivityviewcontroller-tutorial-sharing-data/18261/3 "2016-08-13T07:30:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![oldfella](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@oldfella](https://forums.kodeco.com/u/oldfella)\
**Post date:** [August 14, 2016, 10:39pm UTC](https://forums.kodeco.com/t/uiactivityviewcontroller-tutorial-sharing-data/18261/4 "2016-08-14T22:39:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kmt](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kmt/32/62864_2.png) [@kmt](https://forums.kodeco.com/u/kmt)\
**Post date:** [November 10, 2016, 6:07pm UTC](https://forums.kodeco.com/t/uiactivityviewcontroller-tutorial-sharing-data/18261/5 "2016-11-10T18:07:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![agent87](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/agent87/32/68763_2.png) [@agent87](https://forums.kodeco.com/u/agent87)\
**Post date:** [November 15, 2016, 5:56pm UTC](https://forums.kodeco.com/t/uiactivityviewcontroller-tutorial-sharing-data/18261/6 "2016-11-15T17:56:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anon19151377](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@anon19151377](https://forums.kodeco.com/u/anon19151377)\
**Post date:** [December 15, 2016, 3:55pm UTC](https://forums.kodeco.com/t/uiactivityviewcontroller-tutorial-sharing-data/18261/7 "2016-12-15T15:55:45Z")

</div>

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. 🙂

---

<div class="post-metadata">

**Author:** ![bass](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bass/32/81494_2.png) [@bass](https://forums.kodeco.com/u/bass)\
**Post date:** [December 25, 2016, 11:49am UTC](https://forums.kodeco.com/t/uiactivityviewcontroller-tutorial-sharing-data/18261/8 "2016-12-25T11:49:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![shiitake](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shiitake/32/53377_2.png) [@shiitake](https://forums.kodeco.com/u/shiitake)\
**Post date:** [January 8, 2017, 2:23pm UTC](https://forums.kodeco.com/t/uiactivityviewcontroller-tutorial-sharing-data/18261/9 "2017-01-08T14:23:50Z")

</div>

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")
        }
    }

```

---

<div class="post-metadata">

**Author:** ![mobiledevwaus](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mobiledevwaus/32/68898_2.png) [@mobiledevwaus](https://forums.kodeco.com/u/mobiledevwaus)\
**Post date:** [June 16, 2017, 11:16pm UTC](https://forums.kodeco.com/t/uiactivityviewcontroller-tutorial-sharing-data/18261/10 "2017-06-16T23:16:45Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![awaisfayyazbp](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/awaisfayyazbp/32/108688_2.png) [@awaisfayyazbp](https://forums.kodeco.com/u/awaisfayyazbp)\
**Post date:** [October 27, 2017, 7:35am UTC](https://forums.kodeco.com/t/uiactivityviewcontroller-tutorial-sharing-data/18261/11 "2017-10-27T07:35:26Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![awaisfayyazbp](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/awaisfayyazbp/32/108688_2.png) [@awaisfayyazbp](https://forums.kodeco.com/u/awaisfayyazbp)\
**Post date:** [October 27, 2017, 9:49am UTC](https://forums.kodeco.com/t/uiactivityviewcontroller-tutorial-sharing-data/18261/12 "2017-10-27T09:49:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [October 27, 2017, 3:58pm UTC](https://forums.kodeco.com/t/uiactivityviewcontroller-tutorial-sharing-data/18261/13 "2017-10-27T15:58:00Z")

</div>

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! :]

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [October 27, 2017, 3:58pm UTC](https://forums.kodeco.com/t/uiactivityviewcontroller-tutorial-sharing-data/18261/14 "2017-10-27T15:58:04Z")

</div>

Closed on October 27, 2017, 3:58pm UTC


