|
| 1 | +# LiveCode Builder Language |
| 2 | + |
| 3 | +## Objective-C block support |
| 4 | + |
| 5 | +The handler `CreateObjcBlockPointerFromHandler` has been added the `objc` module |
| 6 | +which facilitates the calling of Objective-C methods that require blocks. |
| 7 | +Objective-C blocks are objects defining fragments of code which can be called at |
| 8 | +a later date, typically used by methods that require progress callbacks or |
| 9 | +completion handlers. |
| 10 | + |
| 11 | +`CreateObjcBlockPointerFromHandler` wraps a LCB handler in an `ObjcBlockPointer` |
| 12 | +and returns true on success and false otherwise. In order to call the |
| 13 | +`CreateObjcBlockPointerFromHandler`, pass the handler to be wrapped as the first |
| 14 | +argument and the variable into which the `ObjcBlockPointer` should be placed as |
| 15 | +the second. |
| 16 | + |
| 17 | + private variable sRequestPermissionsCompletionHandler as optional ObjcBlockPointer |
| 18 | + private variable sTarget as ScriptObject |
| 19 | + |
| 20 | + public handler AudioLibraryInitialize() returns Boolean |
| 21 | + if not CreateObjcBlockPointerFromHandler(RequestPermissionsCompletionHandler, \ |
| 22 | + sRequestPermissionsCompletionHandler) then |
| 23 | + put nothing into sRequestPermissionsCompletionHandler |
| 24 | + return false |
| 25 | + end if |
| 26 | + put the caller into sTarget |
| 27 | + return true |
| 28 | + end handler |
| 29 | + |
| 30 | + public handler RequestPermissionsCompletionHandler(in pBlock as ObjcBlockPointer, in pGranted as CBool) |
| 31 | + post "AudioLibraryRequestPermissionsCallback" to sTarget with [pGranted] |
| 32 | + end handler |
| 33 | + |
| 34 | +In the example above, the handler `RequestPermissionsCompletionHandler` is |
| 35 | +wrapped by the `ObjcBlockPointer` `sRequestPermissionsCompletionHandler`. |
| 36 | + |
| 37 | +The first parameter of the wrapped handler should be an `ObjcBlockPointer`. The |
| 38 | +remaining parameters should match those of the Objective-C block the |
| 39 | +`ObjcBlockPointer` will be used with. |
| 40 | + |
| 41 | +Once created, an `ObjcBlockPointer` can be used to call Objective-C methods that |
| 42 | +require blocks, passing the created `ObjcBlockPointer` where an Objective-C |
| 43 | +block would be expected. |
| 44 | + |
| 45 | + private foreign handler ObjC_AVCaptureDeviceRequestAccessForMediaType(in pMediaType as ObjcId, in pCompletionHandler as ObjcBlockPointer) \ |
| 46 | + returns nothing \ |
| 47 | + binds to "objc:AVCaptureDevice.+requestAccessForMediaType:completionHandler:" |
| 48 | + |
| 49 | + public handler AudioLibraryRequestPermissions() |
| 50 | + unsafe |
| 51 | + ObjC_AVCaptureDeviceRequestAccessForMediaType(StringToNSString("soun"), sRequestPermissionsCompletionHandler) |
| 52 | + end unsafe |
| 53 | + end handler |
| 54 | + |
| 55 | +In the example above, the handler `AudioLibraryRequestPermissions` uses the |
| 56 | +previously created block pointer, `sRequestPermissionsCompletionHandler`, when |
| 57 | +calling the `requestAccessForMediaType:completionHandler:` method of the |
| 58 | +`AVCaptureDevice` type. The method expects the second argument, |
| 59 | +`completionHandler:`, to be a block that takes a single `BOOL` parameter, which |
| 60 | +in this case matched the signature of the handler |
| 61 | +`RequestPermissionsCompletionHandler`. |
| 62 | + |
| 63 | +The wrapped handler will be called whenever the block is invoked, with the first |
| 64 | +parameter being the `ObjcBlockPointer` used to wrap the handler. |
| 65 | + |
| 66 | +In the above example, the `RequestPermissionsCompletionHandler` handler will be |
| 67 | +called when the `completionHandler:` block is invoked, which in this case will |
| 68 | +be when the request permissions process has completed. |
| 69 | + |
| 70 | +The lifetime of a created `ObjcBlockPointer` is not automatically managed. When |
| 71 | +such a value has no more references to it and it is no longer going to be used, |
| 72 | +`DeleteObjcBlockPointer` should be used to free the resources used by it. |
| 73 | + |
| 74 | + public handler AudioLibraryFinalize() |
| 75 | + if sRequestPermissionsCompletionHandler is not nothing then |
| 76 | + DeleteObjcBlockPointer(sRequestPermissionsCompletionHandler) |
| 77 | + put nothing into sRequestPermissionsCompletionHandler |
| 78 | + end if |
| 79 | + end handler |
0 commit comments