Using Crosslight Location Service in iOS 8

By Yudhiy Ariawan | August 15, 2016

Using Crosslight Location Service

Location service provides a streamlined way to access location API that conforms to each the device’s platform. In order to get the current location of the device, we use following snippet code:

this.MobileService.Location.GetCurrentLocation(LocationAccuracy.Best,
    (locationResult) =>
    {
        if (locationResult.Error == null)
		{
		}
        else
		{
		}
    });

This method translate to, get the current location of the device, with best accuracy.

Problem: My app doesn’t returns current location. Why?

Some of you might found out that the code fails silently. You will get no error or warning, but it will never get a location update. The app will never even ask for permission to use location.

It took me a while until I found that we need to add a key to Info.plist and request authorization from the location manager asking it to start. It’s an iOS 8 (and later) related issue. The key, NSLocationWhenInUseUsageDescription, is required when you request authorization for location services. If this key is not present, the system ignores your request and prevents your app from using location services.

Solution: Asking user’s permission to access their location

So we’ll need to add the key to the Info.plist file. It takes a string which is a description of why the app need location services. We can enter a string like “Location is required to find out where you are”.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	...
	<key>NSLocationWhenInUseUsageDescription</key>
	<string>Location is required to find out where you are.</string>
</dict>
</plist>

Save the changes and re-run the app. Your app will now requesting permission by showing a popup.

NSLocationWhenInUseUsageDescription

Hopefully this post may helps developers using location services on iOS 8 (or later). If you have questions or feedback please feel free to submit in the comments section below.

Regards,
Yudi

 

Tags: Crosslight, Development, iOS, Tips and Tricks, Location Service

Comment (0)

There is no comment available.
Name
Email (hidden)
Comment
Previous Next