[swift] Swift로 디바이스의 충전 상태 가져오기
충전 상태 가져오기
iOS에서 충전 상태를 가져오기 위해서는 UIDevice
클래스를 사용해야 합니다. 다음 코드를 참고하여 현재 충전 상태를 가져올 수 있습니다.
import UIKit
UIDevice.current.isBatteryMonitoringEnabled = true
if UIDevice.current.batteryState == .unplugged {
print("배터리가 연결되어 있지 않습니다.")
} else if UIDevice.current.batteryState == .charging {
print("배터리가 충전 중입니다.")
} else if UIDevice.current.batteryState == .full {
print("배터리가 완전히 충전되었습니다.")
} else {
print("충전 상태를 알 수 없습니다.")
}
위 코드를 실행하면 현재 디바이스의 충전 상태를 출력할 수 있습니다.
참고 자료
- Apple Developer Documentation - UIDevice
- Apple Developer Documentation - UIDeviceBatteryState
- Apple Developer Documentation - isBatteryMonitoringEnabled
위 자료들은 디바이스의 충전 상태를 가져오는 데 도움이 될 수 있는 공식 문서입니다. 자세한 내용을 알고 싶다면 참고하시기 바랍니다.