# Sharing a file

**URL:** <https://forum.xojo.com/t/sharing-a-file/51271>\
**Category:** iOS\
**Created:** [November 22, 2019, 7:57pm UTC](https://forum.xojo.com/t/sharing-a-file/51271 "2019-11-22T19:57:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Halford](https://forum.xojo.com/user_avatar/forum.xojo.com/chris_halford/32/480_2.png) [@Chris\_Halford](https://forum.xojo.com/u/Chris_Halford)\
**Post date:** [November 22, 2019, 7:57pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/1 "2019-11-22T19:57:43Z")

</div>

Is it possible to use the sharing panel to share a file?  
I’m guessing I could upload a file to a server and pass the URL, but I’d like to be able to share the file itself.

Optionally I can send some text representing the file but then I suspect anyone receiving it will have a mess to deal with.

Any suggestions?

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [November 22, 2019, 8:37pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/2 "2019-11-22T20:37:35Z")

</div>

Drag a sharing panel to a form.

Then in code, do something like this: (I use this code to create and share a CSV file)

[code]SharingPanel1.ShareText thetextvariable, self, me

exception  
[/code]

---

<div class="post-metadata">

**Author:** ![Chris\_Halford](https://forum.xojo.com/user_avatar/forum.xojo.com/chris_halford/32/480_2.png) [@Chris\_Halford](https://forum.xojo.com/u/Chris_Halford)\
**Post date:** [November 22, 2019, 10:08pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/3 "2019-11-22T22:08:01Z")

</div>

Thats just text though.  
Wouldnt it show up in the body of an email just as a long string?

---

<div class="post-metadata">

**Author:** ![Jason\_King](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@Jason\_King](https://forum.xojo.com/u/Jason_King)\
**Post date:** [November 22, 2019, 10:28pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/4 "2019-11-22T22:28:43Z")

</div>

With iOSKit you can use the UI classes bare metal. It works but Im not sure that an example project exists

---

<div class="post-metadata">

**Author:** ![Chris\_Halford](https://forum.xojo.com/user_avatar/forum.xojo.com/chris_halford/32/480_2.png) [@Chris\_Halford](https://forum.xojo.com/u/Chris_Halford)\
**Post date:** [November 22, 2019, 10:43pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/5 "2019-11-22T22:43:33Z")

</div>

I will take a look. Thanks, Jason.

---

<div class="post-metadata">

**Author:** ![Chris\_Halford](https://forum.xojo.com/user_avatar/forum.xojo.com/chris_halford/32/480_2.png) [@Chris\_Halford](https://forum.xojo.com/u/Chris_Halford)\
**Post date:** [November 24, 2019, 3:44pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/6 "2019-11-24T15:44:05Z")

</div>

@Jason King , I’m not seeing which classes you are referring to. Can you be a bit more specific?

---

<div class="post-metadata">

**Author:** ![Jason\_King](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@Jason\_King](https://forum.xojo.com/u/Jason_King)\
**Post date:** [November 24, 2019, 9:31pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/7 "2019-11-24T21:31:59Z")

</div>

` dim controller as UIActivityViewController
dim url as Foundation.NSURL = Foundation.NSURL.FileURLWithPath(myFile.Path)
controller = new UIActivityViewController(NSArray.CreateWithObject(url),nil)
//present with nil completion handler
self.PresentViewController(controller, True, nil)`

---

<div class="post-metadata">

**Author:** ![Chris\_Halford](https://forum.xojo.com/user_avatar/forum.xojo.com/chris_halford/32/480_2.png) [@Chris\_Halford](https://forum.xojo.com/u/Chris_Halford)\
**Post date:** [November 24, 2019, 9:33pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/8 "2019-11-24T21:33:39Z")

</div>

Do you know if there are any limitations or restriction on where the file can be located to to this?  
Thank you very much for taking the time to write it up.

---

<div class="post-metadata">

**Author:** ![Jason\_King](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@Jason\_King](https://forum.xojo.com/u/Jason_King)\
**Post date:** [November 24, 2019, 9:36pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/9 "2019-11-24T21:36:29Z")

</div>

Not sure. If you put it in the SpecialFolder.documents folder I think you should be fine

---

<div class="post-metadata">

**Author:** ![Chris\_Halford](https://forum.xojo.com/user_avatar/forum.xojo.com/chris_halford/32/480_2.png) [@Chris\_Halford](https://forum.xojo.com/u/Chris_Halford)\
**Post date:** [November 24, 2019, 9:58pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/10 "2019-11-24T21:58:51Z")

</div>

Well that works like a charm! Thanks

This begs the next question of how to register a file type so it will open with my app.  
I’ll browse the forum for this one.

---

<div class="post-metadata">

**Author:** ![Jason\_King](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@Jason\_King](https://forum.xojo.com/u/Jason_King)\
**Post date:** [November 24, 2019, 10:06pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/11 "2019-11-24T22:06:05Z")

</div>

Welcome. I don’t think you’ll find registering file types on the forum sadly. Apples directions are here: [https://developer.apple.com/library/archive/qa/qa1587/\_index.html](https://developer.apple.com/library/archive/qa/qa1587/_index.html)

You might have to do it in Xcode to make your plist correct since I’m pretty sure its just modifying the plist. But I haven’t done this in Xojo before.

---

<div class="post-metadata">

**Author:** ![Chris\_Halford](https://forum.xojo.com/user_avatar/forum.xojo.com/chris_halford/32/480_2.png) [@Chris\_Halford](https://forum.xojo.com/u/Chris_Halford)\
**Post date:** [November 25, 2019, 4:06am UTC](https://forum.xojo.com/t/sharing-a-file/51271/12 "2019-11-25T04:06:40Z")

</div>

That was a good reference and helped me understand what I need to do. I did use Xcode and it was a PITA and didn’t work.  
I did, however find this link [](https://stackoverflow.com/questions/45154373/creating-custom-uti-for-use-with-airdrop-ios) and could hack that to work.

Almost

Now I can send myself the file and it shows up with my application icon (cool).  
By messages, it won’t open.  
By mail, I can tap on it, and can get to my app via the More… button.

1. how do I get my app to show up in the default list as there is no ambiguity with the file extension
2. when I do send it to my app, I have zero idea how to get an event to fire with any data that I can process.

Anyone able to help?

---

<div class="post-metadata">

**Author:** ![Jason\_King](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@Jason\_King](https://forum.xojo.com/u/Jason_King)\
**Post date:** [November 25, 2019, 5:19am UTC](https://forum.xojo.com/t/sharing-a-file/51271/13 "2019-11-25T05:19:02Z")

</div>

> [@464567:@Chris Halford](#):
>
> how do I get my app to show up in the default list as there is no ambiguity with the file extension

Not sure. Its probably on SO though

> [@464567:@Chris Halford](#):
>
> when I do send it to my app, I have zero idea how to get an event to fire with any data that I can process.

In the open event of the app, create a Foundation.NSDictionary object from the pointer passed to you. This will be the launch options dictionary. Then you need to inspect this key to get the URL of the file to open: [UIApplicationLaunchOptionsURLKey | Apple Developer Documentation](https://developer.apple.com/documentation/uikit/uiapplicationlaunchoptionsurlkey?language=objc)  
The string value is “UIApplicationLaunchOptionsURLKey” which you can find with a swift playground in Xcode.

Untested but should be something like:

`dim d as new Foundation.NSDictionary(launchOptionsHandle)
dim fileURL as new Foundation.NSURL(d.Value("UIApplicationLaunchOptionsURLKey"))
dim f as new xojo.io.folderitem(fileURL.path) //might be absoluteString, I don't remember
// process the file`  
You also need to have some error checking so this doesnt crash if the key isnt present. But that shouldnt be difficult

---

<div class="post-metadata">

**Author:** ![Chris\_Halford](https://forum.xojo.com/user_avatar/forum.xojo.com/chris_halford/32/480_2.png) [@Chris\_Halford](https://forum.xojo.com/u/Chris_Halford)\
**Post date:** [November 25, 2019, 12:12pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/14 "2019-11-25T12:12:49Z")

</div>

That looks like it’s on the right track, but this line fails, and I can’t figure out what the key value should be. Ptr to what?  
The compiler complains about the text literal.

> [@](#):
>
> Parameter “Key” expects type Ptr, but this is type TextLiteral

```auto
Dim fileURL As New Foundation.NSURL(d.Value("UIApplicationLaunchOptionsURLKey"))
```

---

<div class="post-metadata">

**Author:** ![Jason\_King](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@Jason\_King](https://forum.xojo.com/u/Jason_King)\
**Post date:** [November 25, 2019, 4:17pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/15 "2019-11-25T16:17:24Z")

</div>

Convert to NSString

---

<div class="post-metadata">

**Author:** ![Chris\_Halford](https://forum.xojo.com/user_avatar/forum.xojo.com/chris_halford/32/480_2.png) [@Chris\_Halford](https://forum.xojo.com/u/Chris_Halford)\
**Post date:** [November 25, 2019, 4:17pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/16 "2019-11-25T16:17:43Z")

</div>

This is the solution:

[code]Dim d As New Foundation.NSDictionary(launchOptionsHandle)

Var k As New Foundation.NSString( “UIApplicationLaunchOptionsURLKey” )

Dim fileURL As New Foundation.NSURL(d.Value(k))

Var t As Text = fileURL.path  
If t.empty = False Then  
Dim f As New xojo.io.folderitem(fileURL.path)  
// deal with the file  
End If[/code]

---

<div class="post-metadata">

**Author:** ![Chris\_Halford](https://forum.xojo.com/user_avatar/forum.xojo.com/chris_halford/32/480_2.png) [@Chris\_Halford](https://forum.xojo.com/u/Chris_Halford)\
**Post date:** [November 25, 2019, 4:19pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/17 "2019-11-25T16:19:33Z")

</div>

@Jason King , I found it and posted the solution just as you sent this.  
Thanks!

It works like a charm.

Now I just need to figure how how to make my app show up in the default list of apps to handle it.

PS: This should be handled right from within Xojo without so much extra jumping through hoops.

---

<div class="post-metadata">

**Author:** ![Chris\_Halford](https://forum.xojo.com/user_avatar/forum.xojo.com/chris_halford/32/480_2.png) [@Chris\_Halford](https://forum.xojo.com/u/Chris_Halford)\
**Post date:** [November 25, 2019, 4:51pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/18 "2019-11-25T16:51:47Z")

</div>

OK, I lied. It only works if the app isn’t currently running. If it’s running the open event never fires again.  
Grrr.

---

<div class="post-metadata">

**Author:** ![Jeremie\_L](https://forum.xojo.com/user_avatar/forum.xojo.com/jeremie_l/32/150_2.png) [@Jeremie\_L](https://forum.xojo.com/u/Jeremie_L)\
**Post date:** [November 25, 2019, 5:07pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/19 "2019-11-25T17:07:27Z")

</div>

I think the ImprovediOSApplication posted by Jason has an OpenURLOptions event for the app that will let you get the URL if the app is already open.

---

<div class="post-metadata">

**Author:** ![Chris\_Halford](https://forum.xojo.com/user_avatar/forum.xojo.com/chris_halford/32/480_2.png) [@Chris\_Halford](https://forum.xojo.com/u/Chris_Halford)\
**Post date:** [November 25, 2019, 5:15pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/20 "2019-11-25T17:15:34Z")

</div>

Hmmm. I’m using his code, which I’ve downloaded recently (most current?) but I can’t find that class in there anywhere. I do see other threads referencing it too.  
This stuff really does need to be integrated right into Xojo.

[Next page](https://forum.xojo.com/t/sharing-a-file/51271.md?page=2)
