# 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:** 2

<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:33pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/21 "2019-11-25T17:33:33Z")

</div>

Alright, I found that project, but as far as I can tell it doesn’t have a way to get the URL.

---

<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:36pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/22 "2019-11-25T17:36:52Z")

</div>

[quote=464620:@Chris Halford]  
PS: This should be handled right from within Xojo without so much extra jumping through hoops.[/quote]

> [@464635:@Chris Halford](#):
>
> This stuff really does need to be integrated right into Xojo.

Everyone agrees on this except Geoff it seems. I had a feedback case for adding the missing Application events that got closed recently.

I think the most recent version of ImprovedIOSApplication is here:

> **[improved iosapplication.xojo\_binary\_project](https://www.dropbox.com/s/ornbgst9rmme6t7/improved%20iosapplication.xojo_binary_project?dl=0)**
>
> Shared with Dropbox

Just copy that class out, the modules folder present wont be up to date. It uses some framework hacks that Xojo says are dangerous but work fine, which is why its not in iOSKit.

---

<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:54pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/23 "2019-11-25T17:54:11Z")

</div>

> [@464637:@Chris Halford](#):
>
> Alright, I found that project, but as far as I can tell it doesn’t have a way to get the URL.

I dont have access to Xojo right now. But if you post the event signatures I can probably point you in the right direction.

---

<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, 6:24pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/24 "2019-11-25T18:24:25Z")

</div>

Jason, that’s the problem. I really have no idea what event signatures I’m looking for.  
Initially I thought that there would be an additional event like in a desktop app.  
I’ve been looking through Apple documentation all morning, but I get lost in that stuff (I’m a Xojo programmer, not an apple programmer).

---

<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 26, 2019, 3:30am UTC](https://forum.xojo.com/t/sharing-a-file/51271/25 "2019-11-26T03:30:35Z")

</div>

Havent tested this, but I believe the following should be approximately what you need. Basically when the app opens you register for a notification that fires if you received a URL. You have to add a handler that receives the callback, and then cleanup code as well so you don’t get a memory leak.

[code]  
// in open event  
declare function addObserver selector “AddObserverForName:object:queue:usingBlock:” lib FoundationLib (obj\_id as ptr, name as CFStringRef, obj as ptr, queue as ptr, block as ptr) as ptr

declare function defaultCenter selector “defaultCenter” lib FoundationLib (clsRef as ptr) as ptr

//declare blk as an iOSBlock instance property  
//declare obsRef as Ptr instance property  
blk = new iOSBlock(AddressOf NotificationCallback)

obsRef = addObserver(defaultCenter(NSClassFromString(“NSNotificationCenter”)), “UIApplicationDidFinishLaunchingNotification”, nil, nil, blk.Handle)

//in close/deactivate event  
declare sub removeObserver selector “removeObserver” lib FoundationLib (obj\_id as ptr, obs as ptr)  
declare function defaultCenter selector “defaultCenter” lib FoundationLib (clsRef as ptr) as ptr

removeObserver(defaultCenter(NSClassFromString(“NSNotificationCenter”)), obsRef)  
obsRef = nil

// new method  
sub NotficationCallback(notification as ptr)  
declare function getUserInfo selector “getUserInfo” lib FoundationLib (obj\_id as ptr) as ptr  
dim userInfo as new NSDictionary(getUserInfo(notification))

```
//access userInfo like the user Info dict from before to initialize the URL/document :)

```

end sub[/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 26, 2019, 7:57pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/26 "2019-11-26T19:57:24Z")

</div>

Thanks Jason,

I’m struggling with this. I fixed the declares (lib I he wrong place).

this line won’t compile because the call back ptr is not an object `blk = new iOSBlock(AddressOf NotificationCallback)`

I assume I should dadd this as a property on the app class? `obsRef`

Finally, there is no close or deactivate event on an iOSApplicaiton, so I cannot add the code recommended.

Xojo staff? This should be easier ☹

---

<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 26, 2019, 8:49pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/27 "2019-11-26T20:49:48Z")

</div>

Yes need to add blk as iosblock and obsRef as Ptr to the app as properties. The close event is from the improved iOS application class

---

<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 26, 2019, 9:30pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/28 "2019-11-26T21:30:07Z")

</div>

I am so sorry to labor this, but there are two issues.

This raises an exception ` blk = iOSBlock( AddressOf NotficationCallback)`  
“Delegate cannot be cast to iOSBlock”

And The version of ImprovedIOSApplication that I have does not implement the closed event. Perhaps there is a newer version out there? I downloaded the one you referenced.

---

<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 26, 2019, 11:48pm UTC](https://forum.xojo.com/t/sharing-a-file/51271/29 "2019-11-26T23:48:39Z")

</div>

You need to have a NEW before iOSBlock. And the event is WillResignActive for close 🙂

---

<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 27, 2019, 12:41am UTC](https://forum.xojo.com/t/sharing-a-file/51271/30 "2019-11-27T00:41:26Z")

</div>

Ahhhhhhhhhhh…

So I have “new” in there, but now it crashes hard on the following line.  
I added the bulk and obsRef to the app class. I assume that’s ok?

`obsRef = addObserver(defaultCenter(NSClassFromString("NSNotificationCenter")), "UIApplicationDidFinishLaunchingNotification", Nil, Nil, blk.Handle)
`

---

<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 27, 2019, 1:02am UTC](https://forum.xojo.com/t/sharing-a-file/51271/31 "2019-11-27T01:02:52Z")

</div>

Open the console app and send the crash log please

---

<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 27, 2019, 1:10am UTC](https://forum.xojo.com/t/sharing-a-file/51271/32 "2019-11-27T01:10:57Z")

</div>

I think this Is the relevant part.

Application Specific Information:  
\*\*\* Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[NSNotificationCenter AddObserverForName:object:queue:usingBlock:]: unrecognized selector sent to instance 0x600002908230’  
terminating with uncaught exception of type NSException  
abort() called  
CoreSimulator 681.15 - Device: iPhone 8 (514F788D-3A16-4D6C-BD40-14D59D8A3057) - Runtime: iOS 13.2 (17B102) - DeviceType: iPhone 8

---

<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 27, 2019, 1:14am UTC](https://forum.xojo.com/t/sharing-a-file/51271/33 "2019-11-27T01:14:17Z")

</div>

I don’t think this helps, but here it is anyway.

Application Specific Backtrace 1:  
0 CoreFoundation 0x00007fff23c4f02e \_\_exceptionPreprocess + 350  
1 libobjc.A.dylib 0x00007fff50b97b20 objc\_exception\_throw + 48  
2 CoreFoundation 0x00007fff23c6ff94 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132  
3 CoreFoundation 0x00007fff23c53dac _ **forwarding** _ + 1436  
4 CoreFoundation 0x00007fff23c55f38 \_CF\_forwarding\_prep\_0 + 120  
5 myPace.debug 0x0000000100079d78 App.Event\_Open%%op + 2136

---

<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 27, 2019, 1:19am UTC](https://forum.xojo.com/t/sharing-a-file/51271/34 "2019-11-27T01:19:44Z")

</div>

[quote=464916:@Chris Halford]I don’t think this helps, but here it is anyway.

Application Specific Backtrace 1:  
0 CoreFoundation 0x00007fff23c4f02e \_\_exceptionPreprocess + 350  
1 libobjc.A.dylib 0x00007fff50b97b20 objc\_exception\_throw + 48  
2 CoreFoundation 0x00007fff23c6ff94 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132  
3 CoreFoundation 0x00007fff23c53dac _ **forwarding** _ + 1436  
4 CoreFoundation 0x00007fff23c55f38 \_CF\_forwarding\_prep\_0 + 120  
5 myPace.debug 0x0000000100079d78 App.Event\_Open%%op + 2136[/quote]  
It does help 🙂  
In the declare for addObserver, remove the capitol A: “addObserverForName:object:queue:usingBlock:” instead

---

<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 27, 2019, 1:31am UTC](https://forum.xojo.com/t/sharing-a-file/51271/35 "2019-11-27T01:31:33Z")

</div>

Not quite there yet.

Application Specific Information:  
\*\*\* Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[NSConcreteNotification getUserInfo]: unrecognized selector sent to instance 0x600000c261f0’  
terminating with uncaught exception of type NSException  
abort() called  
CoreSimulator 681.15 - Device: iPhone 8 (514F788D-3A16-4D6C-BD40-14D59D8A3057) - Runtime: iOS 13.2 (17B102) - DeviceType: iPhone 8

Application Specific Backtrace 1:  
0 CoreFoundation 0x00007fff23c4f02e \_\_exceptionPreprocess + 350  
1 libobjc.A.dylib 0x00007fff50b97b20 objc\_exception\_throw + 48  
2 CoreFoundation 0x00007fff23c6ff94 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132  
3 CoreFoundation 0x00007fff23c53dac _ **forwarding** _ + 1436  
4 CoreFoundation 0x00007fff23c55f38 \_CF\_forwarding\_prep\_0 + 120  
5 myPace.debug 0x000000010007b321 App.NotificationCallback%%op + 1217  
6 myPace.debug 0x000000010007c094 Delegate.IM\_Invoke%%p + 52  
7 Foundation 0x00007fff2572b45e -[\_\_NSObserver \_doit:] + 287  
8 CoreFoundation 0x00007fff23b7936c **CFNOTIFICATIONCENTER\_IS\_CALLING\_OUT\_TO\_AN\_OBSERVER** + 12  
9 CoreFoundation 0x00007fff23b787e5 \_CFXRegistrationPost1 + 421  
10 CoreFoundation 0x00007fff23b78551 \_\_\_CFXNotificationPost\_block\_invoke + 193  
11 CoreFoundation 0x00007fff23c76873 -[\_CFXNotificationRegistrar find:object:observer:enumerator:] + 1811  
12 CoreFoundation 0x00007fff23b77ea6 \_CFXNotificationPost + 950  
13 Foundation 0x00007fff2572bc07 -[NSNotificationCenter postNotificationName:object:userInfo:] + 59  
14 UIKitCore 0x00007fff478482f9 -[UIApplication \_callInitializationDelegatesWithActions:forCanvas:payload:fromOriginatingProcess:] + 4334  
15 UIKitCore 0x00007fff4784dce6 -[UIApplication \_runWithMainScene:transitionContext:completion:] + 1281  
16 UIKitCore 0x00007fff46f843c9 -[\_UISceneLifecycleMultiplexer completeApplicationLaunchWithFBSScene:transitionContext:] + 122  
17 UIKitCore 0x00007fff47471be1 \_UIScenePerformActionsWithLifecycleActionMask + 83  
18 UIKitCore 0x00007fff46f84edb \_\_101-[\_UISceneLifecycleMultiplexer \_evalTransitionToSettings:fromSettings:forceExit:withTransitionStore:]\_block\_invoke + 198

---

<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 27, 2019, 1:54am UTC](https://forum.xojo.com/t/sharing-a-file/51271/36 "2019-11-27T01:54:03Z")

</div>

Whoops I switched things in my head. You need to also change this declare to

```auto
declare function getUserInfo lib FoundationLib selector "userInfo" (obj_id as ptr) as ptr
```

---

<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 27, 2019, 2:14am UTC](https://forum.xojo.com/t/sharing-a-file/51271/37 "2019-11-27T02:14:52Z")

</div>

Close, but no cigar (yet)  
I had this working in the open event. Should I be doing it the same here (the part where I get the URL from the incoming ptr?  
I’m wondering if we need to check if the key exists before trying to get it. It bombs on the line:

```auto
 Var k As New Foundation.NSString( "UIApplicationLaunchOptionsURLKey" )
```

The code looks like this (Mostly yours)

[code]Public Sub NotificationCallback(notification as ptr)  
Declare Function getUserInfo Lib FoundationLib selector “userInfo” (obj\_id As ptr) As ptr  
Dim userInfo As New NSDictionary(getUserInfo(notification))

//access userInfo like the user Info dict from before to initialize the URL/document 🙂

Dim d As New Foundation.NSDictionary(notification)  
Var k As New Foundation.NSString( “UIApplicationLaunchOptionsURLKey” )  
Dim fileURL As New Foundation.NSURL(d.Value(k))  
Var t As Text = fileURL.path

// get the file…

End Sub  
[/code]

Crash looks ike this:

[quote]Application Specific Information:  
\*\*\* Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[NSConcreteNotification count]: unrecognized selector sent to instance 0x600000c15680’  
terminating with uncaught exception of type NSException  
abort() called  
CoreSimulator 681.15 - Device: iPhone 8 (514F788D-3A16-4D6C-BD40-14D59D8A3057) - Runtime: iOS 13.2 (17B102) - DeviceType: iPhone 8

Application Specific Backtrace 1:  
0 CoreFoundation 0x00007fff23c4f02e \_\_exceptionPreprocess + 350  
1 libobjc.A.dylib 0x00007fff50b97b20 objc\_exception\_throw + 48  
2 CoreFoundation 0x00007fff23c6ff94 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132  
3 CoreFoundation 0x00007fff23c53dac _ **forwarding** _ + 1436  
4 CoreFoundation 0x00007fff23c55f38 \_CF\_forwarding\_prep\_0 + 120  
5 myPace.debug 0x00000001001ee69c Foundation.NSDictionary.Count.Get%i8%o\<Foundation.NSDictionary\>i4 + 428  
6 rbframework.dylib 0x00000001004338eb RuntimeDebuggerRegisterFrame + 8493  
7 rbframework.dylib 0x0000000100533829 XojoCriticalSection\_TryEnter + 558  
8 rbframework.dylib 0x000000010042e8cd BreakOnExceptions + 13485  
9 rbframework.dylib 0x000000010042b45f BreakOnExceptions + 63  
10 rbframework.dylib 0x0000000100542a35 iOSGraphics\_Translate + 3976  
[/quote]

---

<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 27, 2019, 2:26am UTC](https://forum.xojo.com/t/sharing-a-file/51271/38 "2019-11-27T02:26:24Z")

</div>

> [@464922:@Chris Halford](#):
>
> Should I be doing it the same here (the part where I get the URL from the incoming ptr?

Yes, kinda

> [@464922:@Chris Halford](#):
>
> I’m wondering if we need to check if the key exists before trying to get it

The Value function returns “nil” if the key is not present. Probably good to check for that. The quick examples I type up don’t have error checking 🙂

The issue with your code is:

```auto
Dim d As New Foundation.NSDictionary(notification)
```

The userInfo dictionary is “d” from before. Just remove this line and change where you read the key from “d” on the next line.

---

<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 27, 2019, 3:26am UTC](https://forum.xojo.com/t/sharing-a-file/51271/39 "2019-11-27T03:26:55Z")

</div>

That was just a mistake on my part. Thanks.

Crashing here on the removeObserver line.  
It’s like a Charlie Chaplin movie. Every time I bend down to pick up my hat, I just kick it half a block down the road! 🙂

[code]Sub WillResignActive() Handles WillResignActive  
Declare Sub removeObserver Lib FoundationLib selector “removeObserver” (obj\_id As ptr, obs As ptr)  
Declare Function defaultCenter Lib FoundationLib selector “defaultCenter” (clsRef As ptr) As ptr

removeObserver(defaultCenter(NSClassFromString(“NSNotificationCenter”)), obsRef)  
obsRef = Nil

End Sub[/code]

Application Specific Information:  
\*\*\* Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[NSNotificationCenter removeObserver]: unrecognized selector sent to instance 0x6000029004d0’  
terminating with uncaught exception of type NSException  
abort() called  
CoreSimulator 681.15 - Device: iPhone 8 (514F788D-3A16-4D6C-BD40-14D59D8A3057) - Runtime: iOS 13.2 (17B102) - DeviceType: iPhone 8

Application Specific Backtrace 1:  
0 CoreFoundation 0x00007fff23c4f02e \_\_exceptionPreprocess + 350  
1 libobjc.A.dylib 0x00007fff50b97b20 objc\_exception\_throw + 48  
2 CoreFoundation 0x00007fff23c6ff94 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132  
3 CoreFoundation 0x00007fff23c53dac _ **forwarding** _ + 1436  
4 CoreFoundation 0x00007fff23c55f38 \_CF\_forwarding\_prep\_0 + 120  
5 myPace.debug 0x000000010007a222 App.Event\_WillResignActive%%o + 466  
6 myPace.debug 0x000000010027f8b5 ImprovediOSApplication.HandleResignActive%%o + 197  
7 myPace.debug 0x000000010027fb62 ImprovediOSApplication.!impl\_appWillResign%%ppp + 562  
8 UIKitCore 0x00007fff47844ef1 -[UIApplication \_deactivateForReason:notify:] + 621

---

<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 27, 2019, 3:30am UTC](https://forum.xojo.com/t/sharing-a-file/51271/40 "2019-11-27T03:30:44Z")

</div>

Another declare typo. Should be “removeObserver:” \<- note the colon

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

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