
CL이 붙어있다.
locationServicesEnabled()를 가장 많이 사용한다.didChangeAuthorization 메소드가 호출됨    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        if CLLocationManager.locationServicesEnabled(){
            
        } else {
            let alert = UIAlertController(title: "알림", message: "위치 서비스를 활성화시켜 주세요.", preferredStyle: .alert)
            
            let settingAction = UIAlertAction(title: "설정", style: .cancel){ _ in
                //string에 저장된 문자열로 url을 만든 다음에 open으로 전달하면 설정 앱으로 바로 이동 가능
                if let url = URL(string: UIApplication.openSettingsURLString){
                    UIApplication.shared.open(url)
                }
            }
            alert.addAction(settingAction)
            
            let cancelAction = UIAlertAction(title: "취소", style: .cancel)
            alert.addAction(cancelAction)
            
            present(alert, animated: true)
        }
    }



authorizationStatus, accuracyAuthorization 속성으로 확인하면 된다.func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        switch manager.authorizationStatus {
        case .notDetermined:
            //사용자가 허가 여부를 결정하지 않은 상태(앱 설치 시 기본 상태)
            break
        case .restricted:
            //스크린타임-콘텐츠 및 개인정보 보호 제한-위치 서비스에서 끄거나, 자녀 앱에서 위치 서비스를 끈 경우
            break
        case .denied:
            //사용자가 허가 요청을 거부, 설정앱에서 위치 서비스를 끈 상태
            break
        case .authorizedAlways:
            //항상 허용
            break
        case .authorizedWhenInUse:
            //앱을 사용할때만 허용
            break
        @unknown default:
            break
        }
    }

CLError.Code.locationUnknown 에러를 보고하고 계속해서 시도한다. func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        //에러를 확인하려면 NSError타입으로 캐스팅해야함.
        let error = error as NSError
        guard error.code != CLError.Code.locationUnknown.rawValue else {return}
        
        print(error)
    }

CLLocation: 위도, 경도, 고도가 저장되어있는 객체locations에는 최소한 하나의 데이터가 저장되어 있음func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let currentLocation = locations.last{
            latitudeLabel.text = "\(currentLocation.coordinate.latitude)"
            longtitudeLabel.text = "\(currentLocation.coordinate.longitude)"
        }
    }


한 번 허용: 일회용 권한으로, 앱을 다시 실행하면 같은 경고창을 표시한다.앱을 사용하는 동안 허용: 앱을 포어그라운드에서 사용할 때만 위치 정보 사용 가능. 앱을 삭제하거나, 설정에서 권한 정보를 바꾸기 전까지 계속 유지 됨.


    override func viewDidLoad() {
        super.viewDidLoad()
        
        manager.delegate = self
        manager.requestWhenInUseAuthorization()
    }



switch manager.authorizationStatus {
...
		case .authorizedAlways:
            manager.startUpdatingLocation()
            break
        case .authorizedWhenInUse:
            manager.requestAlwaysAuthorization()
        @unknown default:
            break
        }

앱을 사용하는 동안 허용을 선택 시, WhenInUse와 Always중 고를 수 있게 표시된다.WhenInUse: 기본적으로 앱이 포어그라운드에서 실행될 때 위치 사용 가능. 백그라운드 서비스를 등록하고 나면 백그라운드에서도 사용 가능. 앱을 완전히 종료하면 위치 관련 사용 불가.Always: 포어그라운드, 백그라운드 상관 없이 항상 위치 사용 가능. 앱을 완전히 종료한 상태에서 새로운 위치 이벤트가 발생하면 앱이 백그라운드에서 시작됨. 따라서, 기본적으로는 WhenInUse로 사용하다가, 필요할 때 Always를 요청하는게 정석이다. 물론 Always를 바로 요청해도 되기는 하나, 그런 경우는 드물다.

info에 추가할 수 있는 키 리스트

NSLocationAlwaysAndWhenInUseUsageDescription: iOS 11 버전 이후 사용
NSLocationUsageDescription: macOS에서 사용
NSLocationWhenInUseUsageDescription: iOS 11 버전 이후 사용
NSLocationAlwaysUsageDescription: iOS 11 버전 이전 사용

didUpdateLocations 델리게이트 메소드에서 locations 로 전달한다.func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        //에러를 확인하려면 NSError타입으로 캐스팅해야함.
        let error = error as NSError
        guard error.code != CLError.Code.locationUnknown.rawValue else {return}
        
        print(error)
        manager.stopUpdatingLocation()
    }

    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        switch manager.authorizationStatus {
        case .notDetermined:
            manager.requestWhenInUseAuthorization()
            break
        case .restricted, .denied:
            let alert = UIAlertController(title: "알림", message: "위치 서비스를 활성화시켜 주세요.", preferredStyle: .alert)
            
            let settingAction = UIAlertAction(title: "설정", style: .cancel){ _ in
                //string에 저장된 문자열로 url을 만든 다음에 open으로 전달하면 설정 앱으로 바로 이동 가능
                if let url = URL(string: UIApplication.openSettingsURLString){
                    UIApplication.shared.open(url)
                }
            }
            alert.addAction(settingAction)
            
            let cancelAction = UIAlertAction(title: "취소", style: .cancel)
            alert.addAction(cancelAction)
            
            present(alert, animated: true)
            break
        case .authorizedAlways, .authorizedWhenInUse:
            manager.startUpdatingLocation()
            break
        @unknown default:
            break
        }
    }
locationManagerDidChangeAuthorization(_:) -> .notDiterminedlocationManagerDidChangeAuthorization(_:) -> .authorizedWhenInUsedidUpdateLocations 호출다시 앱을 시작할 경우
1. CL매니저 생성과 함께 locationManagerDidChangeAuthorization(_:) -> .authorizedWhenInUse
2. didUpdateLocations 호출
didUpdateLocations는 위치가 조금이라도 움직여도 호출되기 때문에


Identifier: User Default에서 key로 사용되어, 유저 디폴트에 접근하여 값을 읽고 쓸 수 있다.String Filename: 지역화에 사용되는 파일
이제는 안 뜬다.
