0

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?

Emmett Harper
  • 239
  • 4
  • 15
  • You should request permission and based on the user's approval you can call that method from. Please checkout this https://stackoverflow.com/questions/39033818/detect-permission-of-media-library-ios – Bhavin Kansagara Sep 27 '18 at 14:07

1 Answers1

2

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks this is exactly what I was looking for – Emmett Harper Sep 27 '18 at 15:34
  • Excellent. For my general solution to the authorization problem, see https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch16p678mediaQuery/ch29p946mediaQuery/ViewController.swift (look at `func checkForMusicLibraryAccess`), but I don't know whether that can be adapted for use with react. – matt Sep 27 '18 at 15:48