본문 바로가기
iOS/흡구오디

[앱 만들기] 위치 권한 승인에따른 상태 처리하기

by 23g 2024. 12. 17.

안녕하세요 3G입니다.

오늘은 어제 하던 것에 이어 3번 권한 승인에 따른 상태 처리를 완성했어요

 

솔직히,,, 왜 때문인지는 아직 잘 모르는 상태

일단 복붙으로 성공

 

차차 공부해보도록 해요

3. 권한 승인 상태 처리

// 위치 권한 상태 변경 시 호출되는 메서드
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        switch status {
        case .authorizedWhenInUse, .authorizedAlways:
            print("위치 권한 승인됨")
            locationManager.startUpdatingLocation()  // 위치 업데이트 시작
        case .denied, .restricted:
            print("위치 권한 거부됨")
        default:
            break
        }
    }
    
    // 위치가 업데이트될 때 호출되는 메서드
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.last else { return }
        
        print("현재 위치: \(location.coordinate.latitude), \(location.coordinate.longitude)")
        
        // 현재 위치로 카메라 이동 (필요한 경우 주석 해제)
        // let currentPosition = NMGLatLng(lat: location.coordinate.latitude, lng: location.coordinate.longitude)
        // let cameraUpdate = NMFCameraUpdate(scrollTo: currentPosition)
        // mapView.mapView.moveCamera(cameraUpdate)
    }
    
    // 위치 업데이트 실패 시 호출되는 메서드
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("위치 업데이트 실패: \(error.localizedDescription)")
    }

    // NMFMapViewCameraDelegate 메서드 (필요한 경우 구현)
    func mapView(_ mapView: NMFMapView, cameraDidChangeByReason reason: Int, animated: Bool) {
        print("카메라 변경 이유: \(reason), 애니메이션: \(animated)")
    }