April 23, 2019

iPhone Geo Location Coordinate Tracking using C# Xamarin.iOS

One Step, Two Step, Three Step

Occasionally you might need to track your customers location coordinates in real-time. You could build an app, but given that iOS and Android are supported by separate programming languages, following that route would mean implementing the code base twice…however, there is a way around that…

Enter “Xamarin”…

Xamarin (originally termed “mono”), allows cross-platform implementations for iOS, Android and Windows Mobile using C# .Net.

“With a C#-shared codebase, developers can use Xamarin tools to write native Android, iOS, and Windows apps with native user interfaces and share code across multiple platforms, including Windows and macOS.”

iPhone Geo Location - C# Xamarin iOS

iPhone Geo Location - C# Xamarin iOS

Let’s Code…

For today’s implementation, we’re going to focus on Xamarin.iOS. Let’s begin by creating a class to handle the geo location events.

iPhone Geo Location - C# Xamarin iOS

iPhone Geo Location - C# Xamarin iOS

Next, we need a LocationManager class that can handle the geo location event updates. Inside our class we need an instance of the CLLocationManager, and a delegate to handle the events.

iPhone Geo Location - C# Xamarin iOS

iPhone Geo Location - C# Xamarin iOS

Next we add a method for starting the location updates and the accuracy configuration.

iPhone Geo Location - C# Xamarin iOS

iPhone Geo Location - C# Xamarin iOS

We then need to set some initial permissions, which happen to be different for iOS 8 vs. iOS 9

iPhone Geo Location - C# Xamarin iOS

iPhone Geo Location - C# Xamarin iOS

Now to configure everything to run in our LocationManager’s constructor.

iPhone Geo Location - C# Xamarin iOS

iPhone Geo Location - C# Xamarin iOS

Now to put our LocationManager to use in a ViewController with an instance of the class.

iPhone Geo Location - C# Xamarin iOS

iPhone Geo Location - C# Xamarin iOS

Next, we need a method for subscribing to he location updates, and one for unsubscribing when the app enters background mode also.

iPhone Geo Location - C# Xamarin iOS

iPhone Geo Location - C# Xamarin iOS

Now in our ViewController’s constructor, we can execute our method call to start the geo location updates.

iPhone Geo Location - C# Xamarin iOS

iPhone Geo Location - C# Xamarin iOS

Before we run our implementation, we need to configure the background mode to allow Geo Location updates (via the info.plist).

iPhone Geo Location - C# Xamarin iOS

iPhone Geo Location - C# Xamarin iOS

Following this, some additional privacy requirements are needed, to ensure we ask our user to allow the geo location updates to occur whilst using the app.

iPhone Geo Location - C# Xamarin iOS

iPhone Geo Location - C# Xamarin iOS

Finally, let’s run our iOS app.

iPhone Geo Location - C# Xamarin iOS

iPhone Geo Location - C# Xamarin iOS

Click to allow the permissions as requested by the app…

iPhone Geo Location - C# Xamarin iOS

iPhone Geo Location - C# Xamarin iOS

Enjoy!