I am using the following block method to call a Photo picker but i want to wait for the user input and then get a response...how is this possible?
I call the method using the code below
[self callmethod:[PHPhotoLibrary sharedPhotoLibrary] withCompletion:^(BOOL success, NSError *error, id responce) {
if(success)
{completion(prevStatus == PHAuthorizationStatusAuthorized);
NSLog(@"==%@",responce); // here i get response once user selected photos
}
}];
The method i call using the previous code
-(void)callmethod:(PHPhotoLibrary*)sharedPhotoLibrary withCompletion:(void(^)(BOOL success, NSError* error, id responce))completion
{
if (@available(iOS 14, *)) {
[sharedPhotoLibrary presentLimitedLibraryPickerFromViewController:self];
}
if (completion){
dispatch_async(dispatch_get_main_queue(), ^{
completion(YES,nil,sharedPhotoLibrary); // here that call when method complete
});
}
}
In iOS 15, there is a rendition that takes a completion handler. But in my tests, there are scenarios where it is not called (i.e., no changes) or is called multiple times (!). IMHO, that violates the “completion handler” informal contract. Besides, that is iOS 15, only. So, I would not be inclined to use the completion handler pattern at all.
As suggested in WWDC 2020 video, Handle the Limited Photos Library in your app, you can use
PHPhotoLibraryChangeObserver. For example, if a view controller: