Seasons gifts from the MonoTouch (iPhone) treasure chest. Background (GPS) tasks and ...
Some year it was… I’m not going to bore you with any details on my private life, let’s just say it was an interesting journey. On the professional plane the best thing was learning to build Apps for the iPhone (and iPad) using Monotouch. The best of the old world in the form of a full C# compiler backed by a good variation of the .net framework; the best of the new world in the form the iPhone which still surprises me every day in its usability. A combination which worked and got my first app in the appstore. As a last post for this year three small gifts from MonoTouch. Extremely simple. Finding them buried under the fluff and confusion was the only hard part.
The iPhone has a limited set of user controls, all wrapped up very well in the MonoTouch.UIKit part of the framework (expand the tree there to browse those docs). Controls which allow user interaction are based on UIControl. According to the docs this is an abstract class. Nevertheless UIControl does show up in Interface Builder (the tool to build an iPhone user Interface) and can be used in a view.
In case you need a simple surface with just a custom background color which can be touched or tapped UIControl is perfectly suited. Create one in interface builder or from code and attach any desired behavior
UIControl myControl = new UIControl (new RectangleF (0, 0, 80, 20));
myControl.BackgroundColor = Watch.BackGroundColor;
myControl.Layer.CornerRadius = 5;
myControl.TouchDown += (sender, e) =>
Controller.NavigationController.PushViewController (new ConfigBackGroundColor (Watch, buttonColor), true);
Simplicity which shines.
iOS 4 brought multitasking support to the iPhone. When the user switches to another app or the iPhone is switched to stand-by mode an app can be suspended. It will resume where it left of upon returning to the foreground. This is supported in the new versions of Monotouch. Any new app will automatically get this behavior. Old apps still restart after an app switch (not when returning from standby). Or they do multitask in the emulator but not on real hardware. To add multitasking behavior to an existing app you have to switch the SDK version to 4.0 for the iPhone platform before rebuilding.
And now your app does multitask.
GPS recording in the background
When your app is sent to the background it is allowed to run some code. This code cannot touch any of the UI and is carefully monitored by the OS. The easy part is being notified of switching to and from the background. That’s done by overriding two member of the Appdelegate.
public partial class AppDelegate : UIApplicationDelegate
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
// The usual stuff
public override void DidEnterBackground (UIApplication application)
LocationManager.TrainingMode = false;
LocationManager.LocationRecorder.AllowUI = false;
public override void WillEnterForeground (UIApplication application)
LocationManager.LocationRecorder.AllowUI = true;
public override void OnActivated (UIApplication application)
A typical background task is recording the GPS position. Your app might not be active but still want to keep track of its position.
The location part of the framework (expand to Monotouch.CoreLocation) works by subscribing. There is one location manager object to which you hook in an event handler. Every time the GPS has a new position to report your handler will be fired. When an app switches to the background this handler is no longer fired. The first thought would be to tickle the location manager from a background task to keep it sending position reports.
The good thing is that there is no need at all to do that. All that is needed is a setting in info.plist, your apps configuration file. Under UIBackgroundModes there are three kinds of tasks which can be kept alive by the OS itself: location, audio and voip.
Having added this entry your apps location recording event handler will keep firing. Even when the phone is set to standby. Just don’t touch the UI and watch your battery
Al of this is almost to simple to be true. I’m looking forward what 2011 will bring. Happy new year.