簡単な質問があります。iPhoneのバッテリー残量を取得するにはどうすればよいですか?
[UIDevice currentDevice] batteryLevel]
簡単ですか?ただし、少し問題があります。 UIKitを使用できません。ここに私がこれまでに書いたものがありますが、うまくいかないと思います:
// notification port
IONotificationPortRef nport = IONotificationPortCreate(kIOMasterPortDefault);
CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(nport), kCFRunLoopDefaultMode);
CFMutableDictionaryRef matching = IOServiceMatching("IOPMPowerSource");
kern_return_t kr = IOServiceAddMatchingNotification(nport, kIOFirstMatchNotification, matching, (IOServiceMatchingCallback)power, self, &powerIterator);
NSLog(@"Kernel said %d",kr);
power((void*)nport,powerIterator);
バッテリーレベルを取得するには、 IOKitに頼る必要があると確信しています。私のアプリケーションは、UIKit を使用できない低レベルのアプリケーションであるため、UIKit を使用しません。私が使用しているフレームワークは次のとおりです。
代替テキスト http://img837.imageshack.us/img837/1829/screenshot20100718at211.png