0

I have a hybrid app that calls a function in iOS (openZipfile()) to open a .zip file natively.
In the function openZipfile() (objective-c), the file is selected via UIDocumentPickerViewController documentPicker* and then opened and read asynchronously via didPickDocumentAtURLs.
I need to wait in openZipfile() until the reading of the file is done.
How do I do this? (I'm not versed with objective-c).

Thanks


EDIT1:

Thanks for the comments.
I read about completion handler and was able to implement the general completion handler pattern similar to here.

This works ok when I use my own custom function, for example
-(void) myMethod:(myCompletion) compblock
But I need to apply the completion handler after the function didPickDocumentAtURL.
This function has a specific signature which needs to adhere to the Apple API i.e. I cannot change it from:

- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url 
{
   ...
}

to:

- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url, (myCompletion) compblock
{
    ...
    compblock(YES);
}

In my code I trigger the document picker with the following line:

[self.VC.navigationController presentViewController:documentPicker animated:YES completion:NULL];

I don't fully understand this line: does the parameter completion:NULL refer to completion handler in any way?
If yes, what is the way to hook it up to the handler?

I also tried to use a semaphore, similar to here but it doesn't work.

So, how can I call a completion handler in the context of the predefined function didPickDocumentAtURL ?

Avner Moshkovitz
  • 1,138
  • 1
  • 18
  • 35
  • 1
    Search for "completion handler" and "callback function" – jnpdx Jan 12 '22 at 03:27
  • `completionBlock` might be also a key word to search along with `async` + `Objective-C`, since it's the type of the parameter (in Swift, it's `closure` for `block`). – Larme Jan 12 '22 at 08:41

0 Answers0