iosobjective-creact-nativepromisempmediaquery

MPMediaQuery wait until Media dialog is complete before callback


My goal is to establish a bridge between iOS and react-native and return back all the podcast titles. The bridge seems to be working, and the podcast titles get returned however the callback I've set up gets called immediately and not after the user has accepted the Media permissions prompt ( The prompt below )

enter image description here

So the callback will return no podcast titles since I'm not authorized, however if I invoke the function after the user has clicked "OK" meaning I'm authorized then the podcast titles return properly. Because I don't have to wait for the dialog to be answered. So my question is how do I wait for the dialog to be answered before calling my callback with the podcast titles.

Here is my function to retrieve the podcast titles:

RCT_EXPORT_METHOD(requestPodcastTitles: (RCTResponseSenderBlock)callback)
{
    MPMediaQuery *query = [[MPMediaQuery alloc] init];
    [query setGroupingType: MPMediaGroupingPodcastTitle];
    NSArray *podcasts = [query collections];
    NSMutableArray *mutablePodcastsToSerialize = [NSMutableArray array];

    for (MPMediaItemCollection *podcast in podcasts) {
      MPMediaItem *representativeItem = [podcast representativeItem];
      NSString *podcastTitle =
      [representativeItem valueForProperty: MPMediaItemPropertyPodcastTitle];\
      NSLog (@" Podcast Title: %@", podcastTitle);
      NSDictionary *podcastDictionary = @{@"podcastTitle": podcastTitle};
      [mutablePodcastsToSerialize addObject:podcastDictionary];

    }

    callback(@[[NSNull null], mutablePodcastsToSerialize]);
    return;
}

Any idea what needs to be done to wait for the prompt to end before returning the callback?


Solution

  • The problem is that you did not call MPMediaLibrary.requestAuthorization(). If you did, it would call you back when the dialog is gone, and now you can proceed.