Black magic and block pointers....?

LiveCode Builder is a language for extending LiveCode's capabilities, creating new object types as Widgets, and libraries that access lower-level APIs in OSes, applications, and DLLs.

Moderators: LCMark, LCfraser

Post Reply
Simon Knight
Posts: 845
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Black magic and block pointers....?

Post by Simon Knight » Mon Nov 28, 2022 5:21 pm

Hi,

I have just spent the day trying to add "authorisation" to my Calendar Event library. I believe that this is required if it is to be of any use on iOS. The objective C is described https://developer.apple.com/documentati ... guage=objc

The objective-c method is

Code: Select all

- (void)requestAccessToEntityType:(EKEntityType)entityType 
                       completion:(EKEventStoreRequestAccessCompletionHandler)completion;
the parameters are described as

Code: Select all

entityType
The event or reminder entity type.

completion
The block to call when the request completes.
The parameter 'completion' references a 'block' which I take to mean that it points at some code. So this suggests that I should use the LCB command

Code: Select all

CreateObjcBlockPointerFromHandler(pHandler,rBlockPtr)

The dictionary includes an example posted below and I would like some help to work out what is happening:

Code: Select all

private variable sRequestPermissionsCompletionHandler as optional ObjcBlockPointer
private variable sTarget as ScriptObject

public handler AudioLibraryInitialize() returns Boolean
	if not CreateObjcBlockPointerFromHandler(RequestPermissionsCompletionHandler, sRequestPermissionsCompletionHandler) then
		put nothing into sRequestPermissionsCompletionHandler
		return false
	end if
	put the caller into sTarget
	return true
end handler

private foreign handler ObjC_AVCaptureDeviceRequestAccessForMediaType(in pMediaType as ObjcId, in pCompletionHandler as ObjcBlockPointer) \
	returns nothing \
	binds to "objc:AVCaptureDevice.+requestAccessForMediaType:completionHandler:"

public handler AudioLibraryRequestPermissions()
	unsafe
		ObjC_AVCaptureDeviceRequestAccessForMediaType(StringToNSString("soun"), sRequestPermissionsCompletionHandler)
	end unsafe
end handler

public handler RequestPermissionsCompletionHandler(in pBlock as ObjcBlockPointer, in pGranted as CBool)
	post "AudioLibraryRequestPermissionsCallback" to sTarget with [pGranted]
end handler
I am struggling to work out what is happening and I am hoping that someone can help.
My thoughts so far :

The aim is to enable the LCB library to use the class method +requestAccessForMediaType:completionHandler: which is a method of the AVCaptureDevice class. The complication is that the completionHandler requires a pointer to a block of code (a handler?) that is called once the method completes (true/false/other)?

So to create this block pointer the public handler AudioLibraryInitalise has to be called first and this creates the block pointer and stores it in the ObjcBlockPointer variable sRequestPermissionsCompletionHandler. It also puts 'the caller' into a variable scriptobject sTarget. I think that sTarget refers to the LCB script (true/false/other)?

The handler AudioLibraryRequestPermissions() calls the Objc class method defined in the private foreign handler. This does whatever it does and on completion calls the final handler RequestPermissionsCompletionHandler as this is what is being pointed at in the block pointer. This posts the text to sTarget, which might be the LCB script, along with pGranted which is type CBoolean. (true/false/other)?

Where did pGranted originate given that the objC foreign handler returns nothing?

best wishes from confused of North Lincolnshire

Simon
best wishes
Skids

Simon Knight
Posts: 845
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Black magic and block pointers....?

Post by Simon Knight » Wed Nov 30, 2022 9:11 am

Just for the record I have spotted my mistyping of the method name. I am now starting again from scratch.

S
best wishes
Skids

Simon Knight
Posts: 845
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Black magic and block pointers....?

Post by Simon Knight » Wed Nov 30, 2022 5:22 pm

Hi,
After much tugging of hair I now have some code that runs, the crucial handler is given below:

Code: Select all

-- define the objective C foreign call to eventstore object method
private foreign handler ObjC_RequestAccessToEventStore(in pID as ObjcID, in pEntityType as CUInt,in pCompletionHandler as ObjcBlockPointer) \
	returns nothing \
	binds to "objc:EventKit>-requestAccessToEntityType:completion:"
Unfortunately while it runs it does not do anything as can be seen from the log file.

Code: Select all

4:13 PM: Compiling module /Users/skids/Programming/LiveCode/LivecodeBuilder_Projects/EventKit/Version106/EventStoreAuthorisationTEST.lcb
4:13 PM: 
4:13 PM: Loaded library 
4:14 PM: "Test of logging"
4:14 PM: "Calling the Access Request"
"Returned from Access Request"
"completion handler runs - pGranted is false"
I looked at some swift code that used the same method and it appeared to display a prompt for the user to grant access. Perhaps it only works on iOS.....

However, setting the access to allowed through system prefs does return the correct result.

Code: Select all

4:19 PM: Compiling module /Users/skids/Programming/LiveCode/LivecodeBuilder_Projects/EventKit/Version106/EventStoreAuthorisationTEST.lcb
4:19 PM: 
4:19 PM: Loaded library 
4:20 PM: "Test of logging"
4:20 PM: "Calling the Access Request"
"Returned from Access Request"
"completion handler runs - pGranted is true"
More searching required.....

S
best wishes
Skids

Simon Knight
Posts: 845
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Black magic and block pointers....?

Post by Simon Knight » Sat Dec 03, 2022 12:42 pm

Most of the issues I was seeing were because I was using LC 10 dp 1. This has build issues. When built in 9.6.8 my test app works - phew.

S
best wishes
Skids

Post Reply

Return to “LiveCode Builder”