[swift] Swift DeviceKit을 사용하여 디바이스의 위치 서비스 활성화 여부 확인하기

먼저, Swift Package Manager를 통해 Swift DeviceKit을 프로젝트에 추가합니다. 프로젝트의 Package.swift 파일에 아래 코드를 추가하여 Swift DeviceKit을 설치합니다.

dependencies: [
    .package(url: "https://github.com/freshOS/DeviceKit.git", from: "4.1.0")
]

Swift DeviceKit을 설치한 후, 다음과 같이 디바이스의 위치 서비스 활성화 여부를 확인할 수 있습니다.

import DeviceKit
import CoreLocation

// 위치 서비스 활성화 여부 확인
func isLocationServiceEnabled() -> Bool {
    let device = Device.current
    
    if case .iPhone = device {
        let locationManager = CLLocationManager()
        return CLLocationManager.locationServicesEnabled()
    }
    
    return false
}

위 코드에서 isLocationServiceEnabled() 함수는 디바이스가 아이폰인 경우에만 위치 서비스 활성화 여부를 확인합니다. CoreLocation 라이브러리의 CLLocationManager 클래스를 사용하여 위치 서비스 활성화 여부를 확인합니다.

위치 서비스가 활성화되어 있으면 true를 반환하고, 비활성화되어 있으면 false를 반환합니다.

해당 코드를 사용하여 디바이스의 위치 서비스 활성화 여부를 쉽게 확인할 수 있습니다.


참고: