iosobjective-cflashlightavcapturedevicerespondstoselector

Best way to handle this error? [AVCaptureFigVideoDevice setTorchModeOnWithLevel:error:]: unrecognized selector sent to instance


I'm trying to turn off an image in my torch app if the device doesn't support LED dimming.

  NSError* outError;
        BOOL success = [device setTorchModeOnWithLevel:brightnessLevel error:&outError];
        if(!success){
            [self.lightDialIndicator setHidden: YES];
            self.lightDial.image = [UIImage imageNamed:@"light_dial_disabled.png"];
        }

but my app crashed with the following error

[AVCaptureFigVideoDevice setTorchModeOnWithLevel:error:]: unrecognized selector sent to instance 0x73ad460

Any idea of a better/working way of detecting when the device doesn't allow me to use setTorchModeOnWithLevel?


Solution

  • First off, setTorchModeOnWithLevel is a property on the AVCaptureDevice class.

    Second, if you want to test if a class can respond to a certain selector that you're calling on it, you use this:

    BOOL isSuccessful = NO;
    if ([device respondsToSelector:@selector(setTorchModeOnWithLevel:error:)]) {
        NSError* outError;
        isSuccessful = [device setTorchModeOnWithLevel:brightnessLevel error:&outError];
    }
    if (!isSuccessful) {
        [self.lightDialIndicator setHidden: YES];
         self.lightDial.image = [UIImage imageNamed:@"light_dial_disabled.png"];
    }
    

    You didn't show how you instantiated device in your example, but this applies to any class where you're unsure of whether it has a certain method or not.