Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@implementation MediaPlayerIOSPodcastTitles
@synthesize bridge = _bridge;
// Export a native module
// https://facebook.github.io/react-native/docs/native-modules-ios.html
RCT_EXPORT_MODULE();
// Export constants
// https://facebook.github.io/react-native/releases/next/docs/native-modules-ios.html#exporting-constants
- (NSDictionary *)constantsToExport
{
return @{
@"EXAMPLE": @"example"
};
}
// Return the native view that represents your React component
- (UIView *)view
{
return [[UIView alloc] init];
}
- (NSArray<NSString *> *)supportedEvents{
return @[@"podcast-titles"];
}
// Export methods to a native module
// https://facebook.github.io/react-native/docs/native-modules-ios.html
RCT_REMAP_METHOD(getPodcastTitles, getPodcastTitlesWithResolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
[MPMediaLibrary requestAuthorization:^(MPMediaLibraryAuthorizationStatus status) {
if (status == MPMediaLibraryAuthorizationStatusAuthorized) {
MPMediaQuery *podcastsQuery = [MPMediaQuery podcastsQuery];
NSMutableSet<NSString*> *podcastNameSet = [NSMutableSet set];
for (MPMediaItem *podcast in podcastsQuery.items){
NSString* unformattedPodcastTitle = podcast.podcastTitle;
if (unformattedPodcastTitle){
NSString *podcastTitle = [unformattedPodcastTitle plusSignEncoding];
[podcastNameSet addObject:podcastTitle];
}
}
NSArray *podcastTitlesArray = [podcastNameSet allObjects];
resolve(podcastTitlesArray);
}else{
reject(@"300", @"Could not access your Podcast Library. Please adjust this in the Settings App", nil);
}
}];
}
// Created by react-native-create-bridge
import { NativeModules } from 'react-native'
const { MediaPlayerIOSPodcastTitles } = NativeModules
export default {
getPodcastTitles () {
return MediaPlayerIOSPodcastTitles.getPodcastTitles()
},
EXAMPLE_CONSTANT: MediaPlayerIOSPodcastTitles.EXAMPLE_CONSTANT
}
#import "NSString+StringUtils.h"
@implementation NSString (StringUtils)
-(NSString*)plusSignEncoding{
NSArray<NSString*>* words = [self componentsSeparatedByString:@" "];
return [words componentsJoinedByString:@"+"];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment