본문 바로가기
Swift

CoreLocation) 위치정보 이용에 필요한 승인 권한 요청(사용중, 항상)

by 김 Duke 2024. 2. 1.

목차

    CoreLocation을 사용하며 위치정보를 이용하기 위해 권한이 필요하다.

     

    애플의 문서에 따르면 위치 데이터는 민감하고 앱 사용자의 개인정보 보호에 영향을 미치기 때문에 섬세한 요청 방식을 요구한다.

    (앱을 사용하면서 위치정보가 필요한 바로 그 시점에 권한을 요청하기를 권유하고 있다. 예를 들어 내비게이션 앱을 키자마자가 아닌, 위치 정보가 필요한 시점)

     

    1. Access Level

    앱이 위치정보에 접근하는 수준에는 두 가지가 있다.

    • 사용중: 앱을 사용하는 동안만 허용
    • 항상: 앱을 사용하지 않을 때도 허용

     

    사용 중: When In Use

    사용 중은 앱을 사용하는 동안에 필요한 위치 정보에 대한 권한이다.

     

    한 번 허용은 앱을 켤 때 마다 권한을 받는 임시허용이다.

    앱을 사용하는 동안 허용은 한 번 받아두면 앱을 삭제하지 않는 이상 권한을 더 받을 필요 없다.

     

     

    항상: Always

    항상은 사용중 수준의 기능은 물론이고, 앱이 종료된 상태라도 자동으로 실행하여 위치 정보에 접근할 수 있다.

    따라서 앱이 위치서비스를 항상 제공할 필요가 있을 때만 권한을 요청해야한다.

     

     

     

    2. 위치정보 서비스 승인 권한 요청

    두 가지 Access Level의 차이에 대해 알아봤으니 권한을 요청하는 방법을 알아보자

     

     

    사용 중: When In Use 권한 요청

    Info.plist파일에 가서 프로퍼티 리스트에 "Privacy - Location When In Use Usage Description"를 추가한다.

    Value 값에 위치 정보를 기반으로 어떤 서비스를 제공하는지 적으면 사용자가 권한요청을 받았을 때 함께 표시된다.

     

     

    이후 CoreLocation의 위치정보를 이용하기 위해 사용하는 CLLocationManager 인스턴스에서 "requestWhenInUseAuthorization" 메소드를 호출해주면 된다. 

    class LocationManager: NSObject {
        let locationManager = CLLocationManager()
    
        override init() {
            super.init()
            locationManager.delegate = self
            locationManager.requestWhenInUseAuthorization()
        }
    }
    
    extension LocationManager: CLLocationManagerDelegate {
    }

     

     

     

    항상: Always 권한 요청

    항상Always 권한 요청은 전자에 비해 복잡하고 문서도 복잡하다.

    마찬가지로 Info.plist파일에 가서 프로퍼티 리스트에 "Privacy - Location Always and When In Use Usage Description" 를 추가한다. 이 때, 사용중 권한 요청에 필요한 "Privacy - Location When In Use Usage Description" 도 꼭 추가해줘야한다. 이유는 아래나온다.

     

     

    항상Always 권한을 받기 위해선, 꼭 사용중의 권한을 받아야한다. 그렇기 때문에 일단 "requestWhenInUseAuthorization" 를 호출한 후 "requestAlwaysAuthorization" 를 호출하여야 한다. 하지만 여기엔 문제가 있다.

     

    아래 코드처럼 연달아 메소드를 호출해도 항상Always권한 요청이 나오지 않는다.

    class LocationManager: NSObject {
        let locationManager = CLLocationManager()
    
        override init() {
            super.init()
            locationManager.delegate = self
            locationManager.requestWhenInUseAuthorization()
            locationManager.requestAlwaysAuthorization()
        }
    }
    
    extension LocationManager: CLLocationManagerDelegate {
    }

     

     

    이에 대해서 requestAlwaysAuthorization 메소드의 문서에 몇가지 방법이 나와있다. 

    내가 사용한 방법은 다음과 같다.

    위처럼 동시에 호출이 아닌, 먼저 사용중WhenIsUse 권한을 요청하고, 텀을 두고 항상Always권한을 요청하는 것이다.

    이를 활용하여 먼저 사용중WhenIsUse 권한을 요청하고 서비스가 필요할 때 항상Always권한을 요청하면 좋을 듯 하다.

    class LocationManager: NSObject {
        let locationManager = CLLocationManager()
    
        override init() {
            super.init()
            locationManager.delegate = self
            locationManager.requestWhenInUseAuthorization()
    
            DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
                self.locationManager.requestLocation()
            }
        }
    }

     

     

    3. 주의 사항

    • 항상Always권한요청이 필요하여 사용중WhenIsUse 권한을 요청할 때 한번만 허용을 통해 임시 권한을 받으면 항상Alwyas권한 요청을 할 수 없다.
    • requestAlwaysAuthorization 문서를 보다보면 아래와 같이 NSLocationAlwaysUsageDescription에 대한 언급이 있다. 내 글을 보고 Info.plist에 프로퍼티를 넣으면 상관 없지만, 문서를 보고 혼동이 올 수 있다. "NSLocationAlwaysUsageDescription" 프로퍼티는 Depreacated되어 사용할 필요가 없다.

     

     

    https://developer.apple.com/documentation/corelocation/requesting_authorization_to_use_location_services

     

    Requesting authorization to use location services | Apple Developer Documentation

    Obtain authorization to use location services and manage changes to your app’s authorization status.

    developer.apple.com

    https://developer.apple.com/documentation/corelocation/cllocationmanager/1620562-requestwheninuseauthorization

     

    requestWhenInUseAuthorization() | Apple Developer Documentation

    Requests the user’s permission to use location services while the app is in use.

    developer.apple.com

    https://developer.apple.com/documentation/corelocation/cllocationmanager/1620551-requestalwaysauthorization

     

    requestAlwaysAuthorization() | Apple Developer Documentation

    Requests the user’s permission to use location services regardless of whether the app is in use.

    developer.apple.com

    https://developer.apple.com/documentation/bundleresources/information_property_list/nslocationalwaysandwheninuseusagedescription

     

    NSLocationAlwaysAndWhenInUseUsageDescription | Apple Developer Documentation

    A message that tells the user why the app is requesting access to the user’s location information at all times.

    developer.apple.com

    https://developer.apple.com/documentation/bundleresources/information_property_list/nslocationalwaysusagedescription

     

    NSLocationAlwaysUsageDescription | Apple Developer Documentation

    A message that tells the user why the app is requesting access to the user's location at all times.

    developer.apple.com

    https://developer.apple.com/documentation/bundleresources/information_property_list/nslocationwheninuseusagedescription

     

    NSLocationWhenInUseUsageDescription | Apple Developer Documentation

    A message that tells the user why the app is requesting access to the user’s location information while the app is running in the foreground.

    developer.apple.com

     


    TOP

    Designed by 티스토리