Tambahkan CLLocationManagerDelegate
ke warisan kelas Anda dan kemudian Anda dapat melakukan pemeriksaan ini:
Versi Swift 1.x - 2.x:
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")
}
Versi Swift 4.x:
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")
}
Versi Swift 5.1
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")
}
manager.locationServicesEnabled()
daripadaCLLocationManager.loationServicesEnabled()
Dipecahkan!authorizationStatus
ini ditetapkan untuknotDetermined
kemudian bukan hanya penebangan akan lebih baik untuk meminta pengguna 'Izinkan / Jangan Biarkan"Dalam tujuan-c
Anda harus melacak pengguna yang sudah ditolak atau tidak ditentukan kemudian meminta izin atau mengirim pengguna ke aplikasi Pengaturan.
-(void)askEnableLocationService { BOOL showAlertSetting = false; BOOL showInitLocation = false; if ([CLLocationManager locationServicesEnabled]) { switch ([CLLocationManager authorizationStatus]) { case kCLAuthorizationStatusDenied: showAlertSetting = true; NSLog(@"HH: kCLAuthorizationStatusDenied"); break; case kCLAuthorizationStatusRestricted: showAlertSetting = true; NSLog(@"HH: kCLAuthorizationStatusRestricted"); break; case kCLAuthorizationStatusAuthorizedAlways: showInitLocation = true; NSLog(@"HH: kCLAuthorizationStatusAuthorizedAlways"); break; case kCLAuthorizationStatusAuthorizedWhenInUse: showInitLocation = true; NSLog(@"HH: kCLAuthorizationStatusAuthorizedWhenInUse"); break; case kCLAuthorizationStatusNotDetermined: showInitLocation = true; NSLog(@"HH: kCLAuthorizationStatusNotDetermined"); break; default: break; } } else { showAlertSetting = true; NSLog(@"HH: locationServicesDisabled"); } if (showAlertSetting) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"Please enable location service for this app in ALLOW LOCATION ACCESS: Always, Go to Setting?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Open Setting", nil]; alertView.tag = 199; [alertView show]; } if (showInitLocation) { [self initLocationManager]; } }
Implementasikan alertView Delegate kemudian kirim pengguna untuk mengaktifkan layanan lokasi jika sudah ditolak oleh pengguna.
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (alertView.tag == 199) { if (buttonIndex == 1) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; } return; } }
Manajer Lokasi Init
-(void)initLocationManager{ self.locationManager = [[CLLocationManager alloc] init]; if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { [self.locationManager requestAlwaysAuthorization]; } }
Harap perhatikan perbedaan kCLAuthorizationStatusAuthorizedAlways dan kCLAuthorizationStatusAuthorizedWhenInUse.
sumber
SWIFT (Per 24 Juli 2018)
if CLLocationManager.locationServicesEnabled() { }
ini akan memberi tahu Anda jika pengguna telah memilih pengaturan untuk permintaan izin lokasi aplikasi
sumber
Ini hanya fungsi 2 baris di Swift 4:
import CoreLocation static func isLocationPermissionGranted() -> Bool { guard CLLocationManager.locationServicesEnabled() else { return false } return [.authorizedAlways, .authorizedWhenInUse].contains(CLLocationManager.authorizationStatus()) }
sumber
Berikut adalah format yang direkomendasikan Apple .
switch CLLocationManager.authorizationStatus() { case .notDetermined: // Request when-in-use authorization initially break case .restricted, .denied: // Disable location features break case .authorizedWhenInUse, .authorizedAlways: // Enable location features break }
Berikut contoh lengkapnya.
Ini termasuk
AlertView
tombol dengan untuk membawa pengguna keSettings
layar jika sebelumnya ditolak aksesnya.import CoreLocation let locationManager = CLLocationManager() class SettingsTableViewController:CLLocationManagerDelegate{ func checkUsersLocationServicesAuthorization(){ /// Check if user has authorized Total Plus to use Location Services if CLLocationManager.locationServicesEnabled() { switch CLLocationManager.authorizationStatus() { case .notDetermined: // Request when-in-use authorization initially // This is the first and the ONLY time you will be able to ask the user for permission self.locationManager.delegate = self locationManager.requestWhenInUseAuthorization() break case .restricted, .denied: // Disable location features switchAutoTaxDetection.isOn = false let alert = UIAlertController(title: "Allow Location Access", message: "MyApp needs access to your location. Turn on Location Services in your device settings.", preferredStyle: UIAlertController.Style.alert) // Button to Open Settings alert.addAction(UIAlertAction(title: "Settings", style: UIAlertAction.Style.default, handler: { action in guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else { return } if UIApplication.shared.canOpenURL(settingsUrl) { UIApplication.shared.open(settingsUrl, completionHandler: { (success) in print("Settings opened: \(success)") }) } })) alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil)) self.present(alert, animated: true, completion: nil) break case .authorizedWhenInUse, .authorizedAlways: // Enable features that require location services here. print("Full Access") break } } } }
sumber
Untuk swift3.0 dan yang lebih baru, jika sering dilakukan pemeriksaan untuk ketersediaan layanan lokasi, buat kelas seperti di bawah ini,
import CoreLocation open class Reachability { class func isLocationServiceEnabled() -> Bool { if CLLocationManager.locationServicesEnabled() { switch(CLLocationManager.authorizationStatus()) { case .notDetermined, .restricted, .denied: return false case .authorizedAlways, .authorizedWhenInUse: return true default: print("Something wrong with Location services") return false } } else { print("Location services are not enabled") return false } } }
dan kemudian gunakan seperti ini di VC Anda
if Reachability.isLocationServiceEnabled() == true { // Do what you want to do. } else { //You could show an alert like this. let alertController = UIAlertController(title: "Location Services Disabled", message: "Please enable location services for this app.", preferredStyle: .alert) let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil) alertController.addAction(OKAction) OperationQueue.main.addOperation { self.present(alertController, animated: true, completion:nil) } }
sumber
Saat Anda memanggil -startLocation, jika layanan lokasi ditolak oleh pengguna, delegasi pengelola lokasi akan menerima panggilan ke -
locationManager:didFailWithError
: dengankCLErrorDenied
kode kesalahan. Ini berfungsi baik di semua versi iOS.sumber
Use of unresolved identifier 'kCLErrorDenied'
. Pikiran?Di Swift 3.0
if (CLLocationManager.locationServicesEnabled()) { locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest if ((UIDevice.current.systemVersion as NSString).floatValue >= 8) { locationManager.requestWhenInUseAuthorization() } locationManager.startUpdatingLocation() } else { #if debug println("Location services are not enabled"); #endif }
sumber
Untuk meminta izin layanan lokasi yang Anda gunakan:
Jika status saat ini belum ditentukan, peringatan akan muncul meminta pengguna untuk mengizinkan akses. Jika akses ditolak, aplikasi Anda akan diberi tahu di CLLocationManagerDelegate, demikian pula jika izin ditolak, Anda akan diperbarui di sini.
Ada dua status terpisah yang perlu Anda periksa untuk menentukan izin saat ini.
CLLocationManager.locationServicesEnabled()
CLLocationManager.authorizationStatus() == .authorizedWhenInUse
Anda dapat menambahkan ekstensi adalah opsi praktis:
extension CLLocationManager { static func authorizedToRequestLocation() -> Bool { return CLLocationManager.locationServicesEnabled() && (CLLocationManager.authorizationStatus() == .authorizedAlways || CLLocationManager.authorizationStatus() == .authorizedWhenInUse) }
}
Ini sedang diakses ketika pengguna pertama kali meminta petunjuk arah:
private func requestUserLocation() { //when status is not determined this method runs to request location access locationManager.requestWhenInUseAuthorization() if CLLocationManager.authorizedToRequestLocation() { //have accuracy set to best for navigation - accuracy is not guaranteed it 'does it's best' locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation //find out current location, using this one time request location will start the location services and then stop once have the location within the desired accuracy - locationManager.requestLocation() } else { //show alert for no location permission showAlertNoLocation(locationError: .invalidPermissions) } }
Berikut delegasinya:
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { if !CLLocationManager.authorizedToRequestLocation() { showAlertNoLocation(locationError: .invalidPermissions) } }
sumber