UIDeviceBatteryStateDidChangeNotification不适用于iOS 8

我正在开发Cordova应用程序,我需要知道电池的初始状态,并在状态改变时收到通知。 我在iPod Touch 5g上使用iOS 8。

我得到的代码: https : //github.com/apache/cordova-plugin-battery-status/blob/master/src/ios/CDVBattery.m ,所以它应该工作…

我在AudioController.mm中这样做:

#import "AudioController.h" #import <UIKit/UIKit.h> #import <AVFoundation/AVAudioSession.h> #import <MediaPlayer/MPMusicPlayerController.h> #import <AudioToolbox/AudioToolbox.h> @implementation AudioController +(void)initializeAudio { //Battery state detection if ([UIDevice currentDevice].batteryMonitoringEnabled == NO) { [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateBatteryStatus:) name:UIDeviceBatteryStateDidChangeNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateBatteryStatus:) name:UIDeviceBatteryLevelDidChangeNotification object:nil]; } //Initial state UIDevice* currentDevice = [UIDevice currentDevice]; UIDeviceBatteryState currentState = [currentDevice batteryState]; batteryState = NO; // UIDeviceBatteryStateUnknown or UIDeviceBatteryStateUnplugged if ((currentState == UIDeviceBatteryStateCharging) || (currentState == UIDeviceBatteryStateFull)) { batteryState = YES; } batteryLevel = 100 * [currentDevice batteryLevel]; } //Battery detection + (void)updateBatteryStatus:(NSNotification*)notification { UIDevice* currentDevice = [UIDevice currentDevice]; UIDeviceBatteryState currentState = [currentDevice batteryState]; batteryState = NO; // UIDeviceBatteryStateUnknown or UIDeviceBatteryStateUnplugged if ((currentState == UIDeviceBatteryStateCharging) || (currentState == UIDeviceBatteryStateFull)) { batteryState = YES; } batteryLevel = 100 * [currentDevice batteryLevel]; } 

在AudioController.h中

 @protocol AudioControllerDelegate; @interface AudioController : NSObject +(void)initializeAudio; + (void)updateBatteryStatus:(NSNotification*)notification; @end 

我正在获取当前状态和级别,但名为updateBatteryStatus的方法未被调用。 这是为什么?

PS:我也在用这个:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioRouteChangeListenerCallback:) name:AVAudioSessionRouteChangeNotification object:nil]; 

检测耳机何时插入并工作。

PS2:我也有其他方法,但那些是与这个问题有关的。