위치 권한 설정 및 정보 얻기

godo·2022년 4월 5일
0

iOS - MapKit

목록 보기
1/5

info.plist 에 추가 해야 하는 것

  • Privacy - Location Always Usage Description
  • Privacy - Location When In Use Usage Description
  • Privacy - Location Always and When In Use Usage Description

이 세가지를 추가해줘야 합니다 ! ! !

자신의 위치 정보 얻기

import CoreLocation

var locationManager: CLLocationManager = {
        let locationManager = CLLocationManager()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest // 정확한 위치 정보 받기
        locationManager.startUpdatingLocation()
        return locationManager
    }()
    
    var currentLocation = CLLocationCoordinate2D()
    
    
...

locationManager.delegate = self

...

extension TravelInfoViewController: CLLocationManagerDelegate {
    
    func getLocationUsagePermission() {
        self.locationManager.requestWhenInUseAuthorization()
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print("최신 정보 받음")
        
        guard let latestLocation = locations.first else {return}
        
        currentLocation = latestLocation.coordinate
        
    }
    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        
        switch manager.authorizationStatus {
        case .authorizedWhenInUse, .authorizedAlways :
            print("권한 설정 됨")
        case .restricted, .notDetermined :
            print("권한 설정 되지 않음")
        case .denied :
            print("권한 요청 거부 됨")
        default :
            print("GPS, Default")
        }
        
    }
}

일단 자신의 위치를 받으로면 CoreLocation 이라는 라이브러리를 이용해야 합니다.

권한을 받고 권한을 받았을 때 자신의 위치를 CLLocationCoordinate2D 형식으로 저장합니다.

profile
☀️☀️☀️

0개의 댓글