How to get battery Status whether it is charging or not ??

I Have return the following code


[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

UIDeviceBatteryState batteryState = [UIDevice currentDevice].batteryState;

if (batteryState == UIDeviceBatteryStateCharging || batteryState == UIDeviceBatteryStateFull)

{

// Pass the test.

flag = 1;


}

else

{


// Fail the test.

flag = 0

}


There is a weird isssue some time i get battery status as unplugged but the device is connected to machine and machine is on at that time why i am not getting status as plugged.

If there is another way to get battery status please provide me.