How to Retrieve Your App Delegate Singleton Instance

Dec 14, 2009   //   by Derek van Vliet   //   Development  //  3 Comments

You should be familiar with the application delegate class if you have ever developed any kind of iPhone app. It is the class that contains your app’s applicationDidFinishLaunching which most if not all apps use as an entry point for execution. You might use it to keep track of application-level state variables or objects, for example.

As such, from time to time you will have a need to retrieve your app’s application delegate instance. You can retrieve it from anywhere within your app by running the following single line of code:

MyAppDelegateClass *app = (MyAppDelegateClass *)[[UIApplication sharedApplication] delegate];

Just be sure to replace “MyAppDelegateClass” with your own iPhone app’s application delegate class name.

  • Pingback: iPhone Development Advent Tips Recap

  • JL

    You need to cast the delegate to your custom application class for this line of code to work correctly:

    MyAppDelegateClass *app = (MyAppDelegateClass *)[[UIApplication sharedApplication] delegate];

  • http://www.getsetgames.com Rob Segal

    Good catch JL. Corrected!

Our Games

What People are Saying

Latest Tweets

FriendFeed