
As readers of this blog know, we at the Institute for Cyber Security are enjoying developing .NET applications for the iPhone using MonoTouch. Since GPS information is so intrinsic to privacy in cyber security, today's Edify Friday covers how easy it is to incorporate GPS data from your iPhone into your MonoTouch applications.
To start off, we'll create a new iPhone MonoTouch Project called HelloWorldGps, which gives us a simple project to start from. To keep things simple, we'll just add a button and label to our MainWindow.xib in Interface Builder. If you're unfamiliar with how to do this, there is a great "Hello World" tutorial found on the MonoTouch site, as well as a whitepaper on using OpenTK with MonoTouch found in an earlier post. We'll call our label "label" and button "button" and give the button a "Click me" text as shown in Figure 1 below:

Figure 1: MainWindow.xib in Interface Designer
Next, we'll click on the "App Delegate" to give the label and button class outlets under the Identity Inspector, then connect the outlets to the UI elements under the Connections Inspector (see Figure 2).

Figure 2: App Delegate Identity and Connection View
That's all we have to do in the Inspector Builder, so we’ll save our file and go back to MonoDevelop. It should compile without issue.
Now we'll want to create our class that handles the GPS updates. We'll create a class called MyLocationManagerDelegate and have it extend one of the MonoTouch.CoreLocation classes named CLLocationManagerDelegate. This is what will give us our GPS functionality and will do our heavy lifting. All we'll do is override the UpdatedLocation method with a message letting us know when we've received a new location (including our first). See Listing 1 below:
using MonoTouch.CoreLocation;
using System;
namespace HelloWorldGps {
public class MyLocationManagerDelegate : CLLocationManagerDelegate {
public override void UpdatedLocation (CLLocationManager manager, CLLocation newLocation, CLLocation oldLocation) {
Console.WriteLine("Found location.");
}
}
}
Listing 1: MyLocationManagerDelegate.cs
Finally, in our AppDelegate class in Main.cs, we'll tie everything together. We'll create a CLLocationManager member (also found in MonoTouch.CoreLocation) and in the method, we'll set its FinishedLaunchingDelegate property to our MyLocationManagerDelegate and call its StartUpdatingLocation method. Lastly, we'll add a delegate to our button so that when it is clicked, it updates our field with our location (or a "No location" message if we haven't received one yet). The AppDelegate class is displayed inListing 2 below.
// The name AppDelegate is referenced in the MainWindow.xib file.
public partial class AppDelegate : UIApplicationDelegate {
private CLLocationManager lm = new CLLocationManager();
public override bool FinishedLaunching (UIApplication app, NSDictionary options) {
lm.Delegate = new MyLocationManagerDelegate();
lm.StartUpdatingLocation();
button.TouchDown += delegate {
string message = "No location update yet";
if (lm.Location != null) {
message = "I am at " + lm.Location.Coordinate.Latitude + " " + lm.Location.Coordinate.Longitude;
}
label.Text = message;
};
// If you have defined a view, add it here:
// window.AddSubview (navigationController.View);
window.MakeKeyAndVisible ();
return true;
}
}
// This method is required in iPhoneOS 3.0
public override void OnActivated (UIApplication application) {
}
}
Listing 2: AppDelegate class in Main.cs
Don't forget to include using "MonoTouch.CoreLocation;" at the top of Main.cs. At this point, our code should compile without issue, and if we compile and run, the iPhone Simulator should fire up, and once the GPS begins broadcasting a message, the application should give you a location similar to Figure 3 below:

Figure 3: GPS Data in your iPhone
That's all there is to it!
Got a topic that you'd like to see covered in Edify Friday? Leave a comment below or email me!
Erhan J. Kartaltepe,
erhan.kartaltepe-at-utsa.edu