#import "ViewController.h"
#import <UIKit/UIKit.h>
@interface viewcontroller ()
@property (nonatomic,readonly) float batterylevel ns_available_ios (3_0);
//0.. 1.0. -1.0 If Uidevicebatterystateunknown it returns a floating-point value between 0.00-1.00.
@end
@implementation Viewcontroller
-(void) viewdidload {
[super viewdidload];
// turn on battery monitoring
[uidevice currentdevice]. batterymonitoringenabled = YES ;
// Get the status of the battery
< Span style= "color: #000000;" > 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 is divided into:
/*
Uidevicebatterystateunknown, Unknown
uidevicebatterystateunplugged,// not charging
uidevicebatterystatecharging,// Charging now
uidevicebatterystatefull,// Full power
*/
if (batterystate = = uidevicebatterystateunknown) {
self. Dllabel . text = [nsstring stringwithformat:@ "%0.0f%%", batterylevel*];
NSLog(@ "Unknow");
}else{
NSLog(@ "know");
// The remaining power is displayed with a label .
self. Dllabel . text = [nsstring stringwithformat:@ "%0.0f%%", batterylevel*];
}
//
[Self getcurrentbatterylevel];
[Uidevice currentdevice].batterymonitoringenabled = YES;
[[Nsnotificationcenter Defaultcenter]
Addobserverforname:uidevicebatteryleveldidchangenotification
Object:nil Queue:[nsoperationqueue Mainqueue]
usingblock:^ (Nsnotification *notification) {
Level have changed
Dl=[uidevice Currentdevice].batterylevel;
//NSLog (@ " battery charge:%.2f", [Uidevice currentdevice].batterylevel);
//
Self.dlLabel.text = [NSString stringwithformat:@ "%f", [Uidevice currentdevice].batterylevel];
//
// }];
Additional setup after loading the view, typically from a nib.
}
This article is from the "zhuoking" blog, make sure to keep this source http://9951038.blog.51cto.com/9941038/1831784
Monitoring of iOS Batteries