हमेशा [myDevice batteryLevel]
लौटने -1 और [myDevice batterystate]
0 लौटा रहा है (डिफ़ॉल्ट मामले में प्रवेश)।सही बैटरी स्तर और बैटरी स्थिति कैसे प्राप्त करें IOS?
मैं सही मूल्य कैसे प्राप्त कर सकता हूं? क्या कोई इसे हल करने में मेरी मदद कर सकता है? नीचे मेरा कोड है। (हमेशा बैटरी लिफ्ट को "-100%" और बैटरी स्थिति "अज्ञात" के रूप में प्रिंट करें)।
कोड:
UIDevice *myDevice = [UIDevice currentDevice];
[myDevice setBatteryMonitoringEnabled:YES];
float batLeft = [myDevice batteryLevel]*100;
int status=[myDevice batteryState];
NSLog(@"level:%0.0f",batLeft);
NSString *status_str;
switch (status)
{
case UIDeviceBatteryStateUnplugged:
{
NSLog(@"UnpluggedKey");
[email protected]"UnPlugged";
break;
}
case UIDeviceBatteryStateCharging:
{
NSLog(@"ChargingKey");
[email protected]"Charging";
break;
}
case UIDeviceBatteryStateFull:
{
NSLog(@"FullKey");
[email protected]"BatteryFul";
break;
}
default:
{
NSLog(@"UnknownKey");
[email protected]"Unknown";
break;
}
}
NSLog(@"Battery status:%@",status_str);
आप डिवाइस या सिम्युलेटर पर परीक्षण कर रहे हैं? सिम्युलेटर पर परीक्षण आपको वह आउटपुट देगा जो अब आप प्राप्त कर रहे हैं। :-) –