Xcode 14.0 업그레이드 후 iOS 16 기기에서
CLLocationManager.locationServicesEnabled()
사용 후 문제점
This method can cause UI unresponsiveness if invoked on the main thread. Instead, consider waiting for the -locationManagerDidChangeAuthorization: callback and checking authorizationStatus first.
해결방안
CLLocationManager.locationServicesEnabled() -> CLLocationManager.authorizationStatus 활용
lazy var locationManager: CLLocationManager = {
let manager = CLLocationManager()
manager.desiredAccuracy = kCLLocationAccuracyBest
return manager
}()
class ..... {
override func viewDidLoad() {
// ...
// if CLLocationManager.locationServicesEnabled() { // 해당 코드 대신 하단 코드 사용
if locationManager.authorizationStatus == .authorizedAlways || locationManager.authorizationStatus == .authorizedWhenInUse {
print("viewdidload 위치 서비스 On 상태")
} else {
print("viewdidload 위치 서비스 Off 상태")
}
// ...
checkLocationPermission()
}
override func didBecomeActive() {
checkLocationPermission()
}
/*
사용자의 위치 정보를 확인할 수 있도록 위치 권한을 확인한다.
*/
func checkLocationPermission() {
switch locationManager.authorizationStatus {
case .authorizedAlways, .authorizedWhenInUse:
// 위치 사용 권한이 허용되어 있음
// ...
locationManager.delegate = self
// ...
case .notDetermined:
// 위치 사용 권한 허용 여부 미결정 상태. 권한을 허용할 것인지 여부를 묻는 팝업이 나타난다.
// ...
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
// ...
case .restricted, .denied:
// 위치 사용 권한이 허용되지 않은 상태.
// ...
@unknown default:
// [개인정보보호] -> [위치 서비스] 활성화 X
// ...
}
view.setNeedsLayout()
}
}
/* extension for CLLocationManager */
extension IntegratedMapViewController : CLLocationManagerDelegate {
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
print("[locationManagerDidChangeAuthorization]")
switch manager.authorizationStatus {
case .authorizedAlways, .authorizedWhenInUse:
// 사용자가 위치 권한 사용을 허용한 상태.
print("CLLocationManagerDelegate - 사용자가 위치 권한 사용을 허용한 상태임")
// ...
case .denied, .restricted, .notDetermined:
// 위치 사용 권한이 허용되지 않은 상태.
print("CLLocationManagerDelegate - 사용자가 위치 권한 사용을 허용하지 않은 상태임")
// ...
@unknown default:
// 알수없는 상태.
// ... error 처리
}
view.setNeedsLayout()
}
}
'Swift' 카테고리의 다른 글
[Swift] WebKit 의 웹 관련 캐시 삭제 하는 방법 (0) | 2022.12.16 |
---|---|
[Swift] Alamofire 통신에서 캐시 사용 안하는 방법 (0) | 2022.11.25 |
[Swift] UIScrollView 내 UITextView 의 Cursor 로 focus scroll 하는 방법 (0) | 2022.11.17 |
[Swift] ScrollView 에서 Button Highlight Delay가 생길 때 (2) | 2022.11.11 |
[Swift] WebKit 의 WKWebView 사용법 (0) | 2022.10.18 |