Sharing a file

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

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

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:

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.

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.

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

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]

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 :frowning:

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

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.

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

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)

Open the console app and send the crash log please

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

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=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 :slight_smile:
In the declare for addObserver, remove the capitol A: “addObserverForName:object:queue:usingBlock:” instead

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

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

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

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:

 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 :slight_smile:

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]

Yes, kinda

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 :slight_smile:

The issue with your code is:

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.

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

[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

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