Gets the current phone's battery charge status and current power
-(void) Viewdidload {
[Super Viewdidload];
// turn on battery monitoring
[Uidevice currentdevice].batterymonitoringenabled = YES;
// Get the status of the battery
Uidevicebatterystate batterystate = [Uidevice currentdevice].batterystate;
get the remaining power range between 0.000000 to 1.000000
CGFloat batterylevel = [Uidevice currentdevice].batterylevel;
the state of battery
[Uidevice Currentdevice].batterystate is divided into:
Uidevicebatterystateunknown, unknown
Uidevicebatterystateunplugged,// not charged
Uidevicebatterystatecharging,// charging
Uidevicebatterystatefull,// full power
if (batterystate = = Uidevicebatterystateunknown) {
NSLog (@ "Unknow");
}else{
NSLog (@ "know");
The remaining power is displayed with a label .
Self.current.text = [NSString stringwithformat:@ "%f", Batterylevel];
}
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Gets the current phone's battery charge status and current power