프로그래밍/iOS
[ios] 배터리 상태 확인하기
삶의안식처
2015. 4. 8. 17:45
위와 같이 시뮬레이터에서는 배터리 잔량이나 현재 상태가 제대로 뜨지 않으나
실 장비에서 테스트 하면 잘 나온다.
빨간 선 설명
소스 맨 위에 라벨과 연결된 선 : 라벨 변수 추가(.h에 하는게 맞지만 파일 왔다갔다하기 불편하므로...)
밑줄 그은 소스 : 라벨 변수 선언한 것을 사용
버튼과 연결한 빨간 네모 : 버튼 클릭 시 동작할 함수
함수 안 소스
[[UIDevice currentDevice]setBatteryMonitoringEnabled:YES]; //배터리 감시 켜기
//라벨 줄 수 여러개 가능하게
[text setNumberOfLines:0];
[text setLineBreakMode:NSLineBreakByWordWrapping];
//배터리 상태에 따른 문구 표시할 배열
NSArray *stateArray=@[@"Battery state is unknown",@"Battery is not plugged into a charging source", @"Battery is charging", @"Battery state is full"];
//배터리 잔량과 상태 표시를 라벨로 표시
text.text=[NSString stringWithFormat:@"Battery level: %0.2f \n Battery State : %@",([UIDevice currentDevice].batteryLevel)*100, stateArray[[UIDevice currentDevice].batteryState]];