iosobjective-cios-camera

Request access for camera in Objective-C


I have this objective-c code taken from this answer to request camera usage on iOS.

[AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted)
    {
      if(granted){
        return true;
      } else {
        return false;
      }
    }];

I get this error:

Cannot initialize a parameter of type 'void (^ _Nonnull)(BOOL)' with an rvalue of type 'bool (^)(BOOL)'

What is the error?


Solution

  • The return type of this completionHandler is void, so your return statements are causing the issue.

    Even if this completion block would have a BOOL return value, it would be the iOS method that would receive the return value; not your code.

    I guess that you want to report the result back to your app. Because this completion block will be called asynchronously, you need to add logic that will pass the result to your app. This can be a delegate call, invoking a method of an object that's you make accessible in the completion handlers, ...

    The fact that it's asynchronous, means that the method in which you're calling this will probably already have returned.

    Reading the documentation be aware that this completion handler can be called on an arbitrary queue and that it's your responsibility to move to the main queue if you're making UI calls.