本文介紹了iOS:如何正確獲取電池電量的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
限時(shí)送ChatGPT賬號(hào)..
我已嘗試正確獲取電池電量.但是那個(gè)值不像iphone狀態(tài)欄中的值.
I have tried to get correctly battery level. But that value is not like value in status bar of iphone.
通過(guò)代碼使用 UIDevice:
by code use UIDevice:
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
[[UIDevice currentDevice] batteryLevel];
請(qǐng)有人幫助我!我需要獲得正確的電池電量,就像在狀態(tài)欄 imdimatery 中一樣.
please someone help me! I need get correctly battery level that like in status bar imdimatery.
推薦答案
Apple 文檔是這樣說(shuō)的:
Well the Apple docs say this:
電池電量范圍從 0.0(完全放電)到 1.0(100% 充電).在訪問(wèn)此屬性之前,請(qǐng)確保已啟用電池監(jiān)控.
Battery level ranges from 0.0 (fully discharged) to 1.0 (100% charged). Before accessing this property, ensure that battery monitoring is enabled.
所以你的代碼應(yīng)該是這樣的:
So your code should be like this:
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
float batteryLevel = [[UIDevice currentDevice] batteryLevel];
//This will give you the battery between 0.0 (empty) and 1.0 (100% charged)
//If you want it as a percentage, you can do this:
batteryLevel *= 100;
希望這會(huì)有所幫助!
這篇關(guān)于iOS:如何正確獲取電池電量的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!