Registering Code
Use the class method +(void) load
for components that need to self register themselves.
Note that the load
method is run on all subclasses and all categories. This is nothing like the the normal method calling behaviour.
Creating a Registry
If components need to register themselves in some kind of container, use the class method +(void) initialise
to create a container to hold the components that are going to register themselves. It seems from my limited testing that initialize
can be called before load
when a load
method uses a class with an initialize
method, which is pretty cool if reliable.
Running Registered Code
If the components should do something at some specific entry point of your App, then at that entry point, grab the registered components from the registry and do that thing. Eg, you might extend you UIApplicationDelegate
's -(BOOL) application:didFinishLaunchingWithOptions:
to actually perform the setup stages the components registered.
In my case, I actually want the registered code to get run every time a specific kind of object is constructed, so I call the registered methods there and let them have the object being constructed.
More references on load
and initialise
Thank you for the answers and comments that let me put this answer together.
Quite a lot of detail from Mike Ash, although I was initially put off by his statements about
load
being "tricky because it runs so early".A very helpful S.O. question on
load
andinitialize
.