iOS 응용 (swift)
[iOS - swift] iOS14+ 위치 권한 설정 (precise)
jake-kim
2020. 11. 8. 22:20

info.plist에 관련 파일 추가

import
import CoreLocation
변수 delegate설정
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
}
// 버튼 클릭 탭시, 권한 요청
locationManager.requestWhenInUseAuthorization() // when in use auth로 요청 등록
권한 수정 시, delegate 함수
// state
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .authorizedAlways, .authorizedWhenInUse:
break
case .restricted, .notDetermined:
locationManager.requestWhenInUseAuthorization()
case .denied:
requirePermission.accept(.location)
@unknown default:
return
}
}
delegate아닌 곳에서 현재 위치 권한 체크
// location state
switch CLLocationManager.authorizationStatus() {
case .authorizedAlways, .authorizedWhenInUse:
break
case .restricted, .notDetermined:
// 아직 결정하지 않은 상태: 시스템 팝업 호출
locationManager.requestWhenInUseAuthorization()
case .denied:
// 거부: 설정 창으로 가서 권한을 변경하도록 유도
requirePermission.accept(.location)
@unknown default:
return
}
if #available(iOS 14.0, *) {
// location accuracy
let accuracyState = CLLocationManager().accuracyAuthorization
switch accuracyState {
case .fullAccuracy:
print("full")
case .reducedAccuracy:
print("reduce")
@unknown default:
print("Unknown")
}
}