import CoreLocation
private let locationManager: CLLocationManager = {
let manager = CLLocationManager()
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.startUpdatingLocation()
return manager
}()
앱이 수신하고자 하는 위치 데이터의 정확성.
사용자의 현재 위치를 보고하는 업데이트 생성을 시작합니다.
stopUpdatingLocation() : 그만 보고해
requestLocation() : 한번 해줘
앱이 사용 중인 동안 위치 서비스를 사용할 수 있는 사용자의 허가를 요청합니다.
앱이 사용 중인 동안 위치 서비스를 사용할 수 있는 사용자의 허가를 요청합니다.
locationManager.requestWhenInUseAuthorization()
백그라운드 상황에서도 위치 정보가 필요한 경우 ( 네비게이션 등 ) 항상 허용을 해야한다.
requestAlwaysAuthorization()
다음과 같이 수정한다.
수정하면 다음과 같이 권한 요청이 뜬다.
var authorizationStatus: CLAuthorizationStatus { get }
switch locationManager.authorizationStatus {
case .authorizedWhenInUse, .authorizedAlways:
print("권한 있음")
case .notDetermined, .restricted, .denied:
print("권한 없음")
locationManager.requestWhenInUseAuthorization()
default:
break;
}
locationManager.delgate = self
func locationManagerDidChangeAuthorization(CLLocationManager)
func locationManager(CLLocationManager, didFailWithError: Error)
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
print("longitude : ",location.coordinate.longitude)
print("latitude : ",location.coordinate.latitude)
}
}
Features -> Location -> Custom Location.. | 37, -122로 변경 | 결과 |
---|---|---|
![]() | ![]() | ![]() |
지렸다 ㄷㄷ