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!
When you call -startLocation, if location services were denied by the user, the location manager delegate will receive a call to -
locationManager:didFailWithError
: with thekCLErrorDenied
error code. This works both in all versions of iOS.SWIFT (As of July 24, 2018)
this will tell you if the user has already selected a setting for the app's location permission request
For swift3.0 and above , if frequent checks are made for the availability of location services, create a class like below,
and then use it like this in your VC
In objective-c
you should track user already denied or not determined then ask for permission or sent user to Setting app.
Implement alertView Delegate then sent user to enable location service if already deny by user.
Init Location Manager
Please note kCLAuthorizationStatusAuthorizedAlways and kCLAuthorizationStatusAuthorizedWhenInUse is difference.
It is just a 2 line function in Swift 4:
In Swift 3.0