Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if location services are enabled

I've been doing some research about CoreLocation. Recently, I encountered a problem that has been covered elsewhere, but in Objective C, and for iOS 8.

I feel kinda silly asking this, but how can you check if location services are enabled using swift, on iOS 9?

On iOS 7 (and maybe 8?) you could use locationServicesEnabled(), but that doesn't appear to be working when compiling for iOS 9.

So how would I accomplish this?

Thanks!

like image 839
Brendan Chang Avatar asked Jan 18 '16 18:01

Brendan Chang


People also ask

Is location enabled Android?

Android UsersAccess your Android Settings menu. Select Location Services. Turn on "Allow Access to my Location."

How do I know if my location is turned on IOS?

The user can enable or disable location services from the Settings app by toggling the Location Services switch in General. You should check the return value of locationServiceEnabled() method before starting location updates to determine whether the user has location services enabled for the current device.


1 Answers

Add the CLLocationManagerDelegate to your class inheritance and then you can make this check:

Import CoreLocation Framework

import CoreLocation 

Swift 1.x - 2.x version:

if CLLocationManager.locationServicesEnabled() {     switch CLLocationManager.authorizationStatus() {     case .NotDetermined, .Restricted, .Denied:         print("No access")     case .AuthorizedAlways, .AuthorizedWhenInUse:         print("Access")     } } else {     print("Location services are not enabled") } 

Swift 4.x version:

if CLLocationManager.locationServicesEnabled() {      switch CLLocationManager.authorizationStatus() {         case .notDetermined, .restricted, .denied:             print("No access")         case .authorizedAlways, .authorizedWhenInUse:             print("Access")      } } else {     print("Location services are not enabled") } 

Swift 5.1 version

if CLLocationManager.locationServicesEnabled() {     switch CLLocationManager.authorizationStatus() {         case .notDetermined, .restricted, .denied:             print("No access")         case .authorizedAlways, .authorizedWhenInUse:             print("Access")         @unknown default:             break     } } else {     print("Location services are not enabled") } 

iOS 14.x

In iOS 14 you will get the following error message: authorizationStatus() was deprecated in iOS 14.0

To solve this, use the following:
private let locationManager = CLLocationManager()  if CLLocationManager.locationServicesEnabled() {     switch locationManager.authorizationStatus {         case .notDetermined, .restricted, .denied:             print("No access")         case .authorizedAlways, .authorizedWhenInUse:             print("Access")         @unknown default:             break     } } else {     print("Location services are not enabled") } 
like image 142
Rashwan L Avatar answered Sep 19 '22 12:09

Rashwan L