MonoTouch UIApplication and AppDelegate explained

Posted by ESCOZ on Tuesday, February 16, 2010

When you create a new MonoTouch project, one of the first lines of code you see is the FinishedLauching() function, in the AppDelegate class. That’s also one of the first things to change when you’re following basically any tutorial. But what exactly is it? And why is it getting called?

When developing iPhone applications with MonoTouch, there’s always a lot of “magic” that happens behind the scenes; one of these is the AppDelegate class. Unlike most WinForms app, where all the events are always generated within your application, in the iPhone the OS generates a lot of notifications that get sent to the application. The AppDelegate object is one of the ways to receive those notifications from the OS (the other being the NSNotificationCenter). During the application startup, the iPhone OS deserializes the main NIB file (created from the MainWindow.xib file), which contains the UIApplication object and the UIApplicationDelegate object; you won’t need to create these objects, the framework takes care of that for you.

Every iPhone application contains one and only one UIApplication instance, which is a singleton and can be accessed via UIApplication.SharedApplication. Its main role is to handle the proper initialization of the application, and to maintain the list of Windows in the application (which contain all the UIViews objects). The UIApplication also helps routing URL requests and even takes care of things like the badge number displayed by the icon and orientation of the app. Anybody developing an iPhone store should consider taking a quick look at this class before releasing the app, to make sure all the settings are correct.

Together with the UIApplication instance, an UIApplicationDelegate is created. Its function is to receive the notifications from the OS that may affect the application. MonoTouch automatically creates a partial C# class called AppDelegate that inherits from the UIApplicationDelegate, which is kept in the Main.cs file. This is very handy, as every application should always implement at least 2 methods, also created automatically by MonoTouch: FinishedLaunching (UIApplication app, NSDictionary options) and OnActivated (UIApplication application). The default code created by MonoTouch can be seen below:

 
public partial class AppDelegate : UIApplicationDelegate
{

    public override bool FinishedLaunching (UIApplication app, NSDictionary options)
        {
            window.AddSubview (navigationController.View);
            window.MakeKeyAndVisible ();

            return true;
        }

        // This method is required in iPhoneOS 3.0
        public override void OnActivated (UIApplication application)
        {
        }
    }
}

Very important here is the FinishedLaunching method, where you should define the objects in your window, and return true. If you’re using Interface Builder, the UIWindow object will already be created (like above), so all you have to do is add subviews to it. Also, be careful about how much work you do on this method: the app will get killed by the OS if you take longer than a few seconds to return (5-6 in my experience, but I couldn’t find a documented number anywhere). So if you’re planning on loading data from the web, or opening a big database, make sure you do that in a separate thread.

There are several other methods in the UIApplicationDelegate class that you should seriously consider implementing:

  • ReceiveMemoryWarning: which is called when the iPhone is running out of memory and you should release memory right away or your app will be killed
  • WillTerminate: called after the user presses the Home button, gives you the opportunity to save the app state so that next time the user comes back the app looks like it was when it was closed. You'll have to write all the logic to do this yourself, but this is essential for a good iPhone HIG "compliant" app.

The UIApplicationDelegate also handles Notifications, with the RegisteredForRemoteNotifications and the ReceivedRemoteNotification methods. To see a full list of methods in MonoDevelop, simply put the cursor after the closing statement of a class and type “override”. The full list of methods available will show up.