Article

Determining the Availability of Location Services

Determine which location services are active and available on the user's device.

Overview

Not all Core Location services are available on every device, and services may be temporarily unavailable in certain circumstances:

  • The device does not have the hardware needed to support the feature.

  • The user disabled location services in the system settings.

  • The user denied access to location services for your app.

  • The user's device is in Airplane Mode and unable to gather location data.

  • Background App Refresh is disabled and you are monitoring regions or using the significant-change location service, which requires that feature.

Before using any specific location service, check the availability of that service using the methods of your CLLocationManager object. Table 1 lists the methods you call and explains any hardware requirements for the corresponding services.

Table 1

Getting the availability of Core Location services

Method

Description

locationServicesEnabled()

Tells whether you can get the geographic coordinate for the user's current location. This service is generally available on all devices. See Getting the User's Location.

significantLocationChangeMonitoringAvailable()

Tells whether you can get the user's location using the signficant-change location service. This service requires specific hardware and may not be available on all devices. See Getting the User's Location.

isMonitoringAvailable(for:)

Tells whether you can use region monitoring to detect entry into or exit from geographic regions or iBeacon regions. Support for geographic region monitoring requires specific hardware and is generally available on all iOS devices. Support for iBeacon region monitoring requires Bluetooth hardware and is generally available on all devices. For information on how to monitor geographic regions, see Monitoring the User's Proximity to Geographic Regions. For information on how to monitor iBeacon regions, see Determining the Proximity to an iBeacon.

headingAvailable()

Tells whether the device is able to provide compass-related headings. This service requires the presence of a magnetometer. See Getting Heading and Course Information.

isRangingAvailable()

Tells whether you can obtain the relative distance to a nearby iBeacon. This service requires Bluetooth hardware and is generally available on all devices. See Determining the Proximity to an iBeacon.

Listing 1 shows how to check for the availability of the standard location service. Based on the availability of those services, you would enable or disable features of your app accordingly.

Listing 1

Checking for the availability of the standard location service

func checkForLocationServices() {
    if locationManager.locationServicesEnabled {
        // Location services are available, so query the user’s location.
    } else {
        // Update your app’s UI to show that the location is unavailable.
    }
}

If you try to start a service that is unavailable, the CLLocationManager object calls one of the failure-related methods of its delegate. For example, if region monitoring is unavailable, it calls the locationManager(_:monitoringDidFailFor:withError:) method.

Topics

Related Topics

Requiring the Presence of Specific Location Services

Declare the location services that your app requires.

See Also

First Steps

Choosing the Authorization Level for Location Services

Choose the appropriate level of access to location data for your app.

class CLLocationManager

The object that you use to start and stop the delivery of location-related events to your app.

protocol CLLocationManagerDelegate

The methods that you use to receive events from an associated location manager object.