title: Wrapping an Objective-C block containing a Bool parameter in Delphi
body: <p>I have written a wrapper for the <strong>LocalAuthentication</strong> Framework in Delphi in order to implement <code>TouchID</code>. The Objective-C method I am having difficulty with is :</p>
  
  <pre><code>- (void)evaluatePolicy:(LAPolicy)policy
         localizedReason:(NSString *)localizedReason
                   reply:(void (^)(BOOL success,
                                   NSError *error))reply
  </code></pre>
  
  <p>The specific issue is with the "reply" code block which returns whether the <code>TouchID</code> was successful or not and any errors that might have occurred, and executes within a separate thread. I have implemented the Objective-C method in Delphi within an instance interface as follows :</p>
  
  <pre><code>procedure evaluatePolicy(policy: LAPolicy; localizedReason: NSString; reply: LAContextReply); cdecl;
  </code></pre>
  
  <p><code>LAContextReply</code> is defined as :</p>
  
  <pre><code>LAContextReply = procedure(success: Pointer; error: Pointer) of Object;
  </code></pre>
  
  <p>In my application I implement this as :</p>
  
  <pre><code>procedure TForm1.TouchIDReply(success: Pointer; error: Pointer);
  begin
      //Show a message indicating that the method executed
      TThread.Synchronize(TThread.CurrentThread, procedure
      begin
          ShowMessage('Blah!');
      end);
  end;
  
  procedure TForm1.Button1Click(Sender: TObject);
  var
      Context : LAContext;
      dieResult : Boolean;
  begin
      Context := TLAContext.Alloc;
      Context := TLAContext.Wrap(Context.init);
      dieResult := Context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, nil);
      if dieResult = true then
      begin
          Context.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, StrToNSStr('Please present thumb!'), TouchIDReply);
      end;
  end;
  </code></pre>
  
  <p>Everything works great and I get presented with the <code>TouchID</code> dialog. After I authenticate with my finger the <code>TouchIDReply</code> method tries to execute.</p>
  
  <p>Defining the "success" variable as Pointer in the anonymous method is the only way I could get the <code>TouchIDReply</code> method to execute and not throw an exception. If I define the "success" variable as Boolean in the anonymous method I will get an "Invalid class typecast" error.</p>
  
  <p>If I try to retrieve the Boolean value from the Pointer parameter "success" in the "TouchIDReply" method by assigning <code>success^</code> to a newly declared Boolean variable I simply get an Access Violation. My assumption now is that, similar to a pointer variable to an object (like the "error" parameter in this case), the pointer refers to the Objective-C side of things and that I should somehow wrap it into a local Delphi variable. But how do I do this? I don't have access to a wrap method, because I am not working with an object. Maybe I am going about this in totally the wrong way?</p>
  
  <p><strong>Update 1</strong></p>
  
  <p>Working....well, sort of!</p>
  
  <p>While in a debug session I discovered that the "success" variable, which is defined as Pointer, contained the value 0x0000000000000001. This was after a successful TouchID validation, so the Pointer value seemed to indicate the Integer representation of a Boolean TRUE. I then tested my theory by tapping the "Cancel" button on the TouchID window on the device and, sure enough, the value of the "success" variable changed to 0x0000000000000000. Boolean FALSE, right? So my solution for the moment is to explicitly cast the "success" Pointer variable as an Integer and then work with that. The obvious question arises, though. Why do I get an "Invalid typecast" exception when defining the "success" variable as Boolean or Integer, but when I define it as a Pointer, it contains the variable's value (in hex), instead of pointing to a memory location of the variable's value? Am I misunderstanding pointers, or what's going on here?</p>