Skip to main content
Inspiring
Oct 24, 2017 4:33 PM UTC
Question

Add code to AppDelegate on iOS

Hey there!

The lifecycle on iOS calles a function in the AppDelegate when starting the App:

application:didFinishLaunchingWithOptions:

I need to add code to this function.

There is a way to inject (overwrite) functions of the AppDelegate with a native extension as suggested here:

Re: UIApplication Delegate

My issue is that "didFinishLaunchingWithOptions" is called obviously before i could even start a native extension that could overwrite that.

Is there a way on modifying the AppDelegate when building the app? Or any other way i could execute my own code within this function?

Any hint appreciated!

Cheers

seilz

This topic is closed to new replies. Start a new post to keep the conversation going.

3 replies

Legend
Oct 24, 2017 6:33 PM UTC

You say "Or any other way i could execute my own code within this function?"
Do you mean native ANE code or actionscript code?

Translate
seilz2kAuthor
Inspiring
Oct 24, 2017 10:53 PM UTC

Native objective c code, yes.

It's crucual, that i invoke a native iOS framwork at this point in time.

Translate
Legend
Oct 24, 2017 11:10 PM UTC

ok.

So you can use method swizzling to override the Application Delegates as per the other thread.

I prefer to use observers instead. This to me is cleaner as you don't override anything. Other ANEs can also observe the same event without conflicts. You are just "listening" to the event.

You can see how I do this here

https://github.com/tuarua/Swift-IOS-ANE#applicationdidfinishlaunching

applicationDidFinishLaunching

The static library contains a predefined +(void)load method in FreMacros.h. This method can safely be declared in different ANEs. It is also called once for each ANE and very early in the launch cycle. In here the SwiftController is inited and onLoad() called. This makes an ideal place to add observers for applicationDidFinishLaunching and any other calls which would normally be added as app delegates, thus removing the restriction of one ANE declaring itself as the "owner".

+(void)load is key here.

Note although you are not calling any code in your ANE from actionscript you can attach and run code in the ANE at launch.
Whilst you may not be using Swift like my project, the methodology is the same.

Translate