투케이2K

368. (ios/swift5) [유틸 파일] getBatteryPercent : 현재 배터리 퍼센트 값 확인 - UIDevice.current.batteryLevel 본문

IOS

368. (ios/swift5) [유틸 파일] getBatteryPercent : 현재 배터리 퍼센트 값 확인 - UIDevice.current.batteryLevel

투케이2K 2023. 11. 8. 20:59
반응형

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : SWIFT5

 

[소스 코드]

 

    // -----------------------------------------------------------------------------------------
    // MARK: - [현재 배터리 퍼센트 값 리턴]
    // -----------------------------------------------------------------------------------------
    func getBatteryPercent() -> String {
        
        /*
        // -----------------------------------------
        [getBatteryPercent 메소드 설명]
        // -----------------------------------------
        1. 현재 배터리 퍼센트 값 리턴
        // -----------------------------------------
        2. 호출 방법 : C_App().getBatteryPercent()
        // -----------------------------------------
        3. 리턴 반환 : 83.0%
        // -----------------------------------------
        */

        
        // [리턴 변수 선언]
        var returnData = ""


        // [로직 처리 실시]
        UIDevice.current.isBatteryMonitoringEnabled = true
        let batteryRemain = UIDevice.current.batteryLevel
        returnData = NSString(format: "%.1f", batteryRemain * 100) as String + "%"

        
        // [로그 출력 실시]
        S_Log._D_(description: "현재 배터리 퍼센트 값 확인", data: [
            "RETURN :: \(returnData)"
        ])
        
        
        // [리턴 데이터 반환 실시]
        return returnData
    }

 

반응형
Comments