iOS Question Calling MTBBarcodeScanner.requestCameraPermissionWithSuccess with NativeObject (iBarcode2-Lib)

b4x-de

Active Member
Licensed User
Longtime User
Hi,

I’m using the BarcodeScanner from iBarcode2 library kindly provided by JanPRO in this post to process QR codes and images taken while scanning the codes.

I need to check for granted camera permission before scanning. I tried by calling this class method in MTBBarcodeScanner class:

Objective-C:
+ (void)requestCameraPermissionWithSuccess:(void (^)(BOOL success))successBlock { … }

My idea was using native objects but I failed due to my missing OBJC skills:

B4X:
Private Sub requestCameraPermission(bcs As BarcodeScanner) As Boolean
    Dim bolGranted As Boolean
    Dim no As NativeObject = bcs
    Dim bl As NativeObject

    Try
        bolGranted = no.GetField("scanner").RunMethodWithBlocks("requestCameraPermissionWithSuccess:", Array(bl.CreateBlock("successBlock", 0, True)))
        Log(bolGranted)
    Catch
        Log(LastException.Message)
    End Try

    Return bolGranted
End Sub

I receive this exception: Method not found: requestCameraPermissionWithSuccess:, target: <MTBBarcodeScanner: 0x281575800>

Could someone please help me with a solution and give my an explanation what I was doing wrong?

Thanks,
Thomas
 
Last edited:

b4x-de

Active Member
Licensed User
Longtime User
I would like to give some more background information on my problem because other users might experience the same problem with iBarcode2 lib.

In some situations Page_Resize event is not raised in my app. In this case also bcsQrCodeReader.Resize is not properly called before actually bcsQrCodeReader.Start is called. If the user does not grant camera permission this leads to an exception in iBarcode2 lib because an assertion failed:
B4X:
*** Assertion failure in -[MTBBarcodeScanner startScanningWithResultBlock:], /Users/J***/Downloads/iBarcode-2/iBarcode/MTBBarcodeScanner.m:238

The resulting exception message in B4i says:
B4X:
Scanning is prohibited on this device. Check requestCameraPermissionWithSuccess: method before calling startScanningWithResultBlock:

Therefore I was looking for a solution so call requestCameraPermissionWithSuccess: via NativeObject to make sure camera permission is granted. Meanwhile I learned that requestCameraPermissionWithSuccess: requires a block as parameter that evaluates to true or false.
Objective-C:
+ (void)requestCameraPermissionWithSuccess:(void (^)(BOOL success))successBlock { //… }

Therefore I try to define a block in B4i and pass it as Parameter when calling the message. I’m unsure how to process the result. I thought it should work this way:
B4X:
bl = bl.CreateBlock("successBlock", 1, True)
noScanner.RunMethodWithBlocks("requestCameraPermissionWithSuccess:", Array(bl))
Wait For successBlock_Event(args() As Object)
bolGranted = args(0)
Log(bolGranted)

But actually it even fails before this code is processed with a message not found exception. I also tried to call other operations from MTBBarcodeScanner like:
B4X:
Method not found: requestCameraPermissionWithSuccess:, target: <MTBBarcodeScanner: 0x281575800>

Also receive this error when I try to call any other methode, like this:
Objective-C:
+ (BOOL)scanningIsProhibited { // … }

It is the same error in this case. It says message not found. Can anyone please help?

Thanks,
Thomas
 
Upvote 0
Top