Ios 从复杂C方法调用Objective-C方法?

Ios 从复杂C方法调用Objective-C方法?,ios,objective-c,Ios,Objective C,我已经设置了一组方法,在设备充电/不充电时,使设备屏幕变暗和变亮,并在视图上方引入黑色UIView 我面临的挑战是从用于检查设备是否正在充电的C方法调用我的Obj C方法(BrightScreen和dimScreen) 如有任何意见/帮助,将不胜感激 -(void)setUpChargingCheck{ [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES]; // Do whatever you want...

我已经设置了一组方法,在设备充电/不充电时,使设备屏幕变暗和变亮,并在视图上方引入黑色UIView

我面临的挑战是从用于检查设备是否正在充电的C方法调用我的Obj C方法(BrightScreen和dimScreen)

如有任何意见/帮助,将不胜感激

-(void)setUpChargingCheck{

    [[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];

    // Do whatever you want...
    if ([[UIDevice currentDevice] batteryState] != UIDeviceBatteryStateUnplugged){
        [self brightenScreen];
    }else{
        [self dimScreen];
    }

    // Registering for a specific notification
    NSString *notificationName = @"com.apple.springboard.fullycharged";
    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
                                    NULL,
                                    yourCustomMethod,
                                    (__bridge CFStringRef)notificationName,
                                    NULL,
                                    CFNotificationSuspensionBehaviorDeliverImmediately);

}

// The custom method that will receive the notification
static void yourCustomMethod(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
    NSString *nameOfNotification = (__bridge NSString*)name;

    if([nameOfNotification isEqualToString:@"com.apple.springboard.fullycharged"])
    {
        if ([[UIDevice currentDevice] batteryState] != UIDeviceBatteryStateUnplugged){
            [[UIScreen mainScreen] setBrightness:1.0];
            // brighten screen here
        }else{
            [[UIScreen mainScreen] setBrightness:0.0];
            // dim screen here
        }
    }
}

-(void)dimScreen{
    [[UIScreen mainScreen] setBrightness:1.0];
    [self.blackoutView removeFromSuperview];
}

-(void)brightenScreen{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;

    [[UIScreen mainScreen] setBrightness:0.0];
    self.blackoutView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
    [self.blackoutView setBackgroundColor:[UIColor blackColor]];
    [self.view addSubview:self.blackoutView];
}

这段代码在哪个类中?为什么不使用
NSNotificationCenter
而不是
CFNotificationCenterAddObserver
?然后整个问题就消失了。除了
observer
之外,如果给定问题中的当前代码,则
NULL
    YourClass *yourClassObject = (__bridge YourClass *)(observer);
    [yourClassObject dimScreen];