Richard Stelling
Richard Stelling

Reputation: 25665

Triggering code execution in iOS when a framework is loaded

How do you get code in a framework to execute when that framework is loaded under iOS?

The application Reveal (http://revealapp.com) uses this technique (combined with listening out for the UIApplicationDidFinishLaunchingNotification notification).

Upvotes: 4

Views: 365

Answers (2)

Sean
Sean

Reputation: 1

Reveal uses the +(void)load method on NSObject.

Upvotes: 0

0xced
0xced

Reputation: 26558

You have two possibilities.

  1. Use a +load method either in one of your own class or add it on a category on an existing class. For example:

    @implementation MyClass
    
    + (void) load
    {
        // Your initialization code
    }
    
    @end
    
  2. Use __attribute__((constructor)) on a function. For example:

    __attribute__((constructor)) void MyLibraryInitialize(void)
    {
        // Your initialization code
    }
    

Beware: both methods will execute your code before the main function is called.

Upvotes: 4

Related Questions