Created
September 22, 2018 12:41
-
-
Save GaborWnuk/aa4c24cf268f5c1a0bf6177ff6ad7d27 to your computer and use it in GitHub Desktop.
AppDelegate.m for React Native Car Play
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "AppDelegate.h" | |
#import <React/RCTBundleURLProvider.h> | |
#import <React/RCTRootView.h> | |
@implementation AppDelegate | |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | |
{ | |
NSURL *jsCodeLocation; | |
jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil]; | |
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation | |
moduleName:@"carplay" | |
initialProperties:nil | |
launchOptions:launchOptions]; | |
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1]; | |
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; | |
UIViewController *rootViewController = [UIViewController new]; | |
rootViewController.view = rootView; | |
self.window.rootViewController = rootViewController; | |
[self.window makeKeyAndVisible]; | |
[[MPPlayableContentManager sharedContentManager] setDataSource:self]; | |
[[MPPlayableContentManager sharedContentManager] setDelegate:self]; | |
[[MPPlayableContentManager sharedContentManager] reloadData]; | |
return YES; | |
} | |
- (MPContentItem *)contentItemAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
MPContentItem *contentItem = [[MPContentItem alloc] initWithIdentifier:[NSString stringWithFormat:@"channel_%ld", [indexPath indexAtPosition:1]]]; | |
NSDictionary *channel = @{}; | |
switch ([indexPath length]) { | |
case 1: | |
contentItem.title = [NSString stringWithFormat:@"Channels"]; | |
contentItem.playable = NO; | |
contentItem.container = YES; | |
break; | |
default: | |
channel = [[self channels] objectAtIndex:[indexPath indexAtPosition:1]]; | |
contentItem.title = channel[@"name"]; | |
contentItem.playable = YES; | |
contentItem.container = YES; | |
contentItem.streamingContent = YES; | |
} | |
return contentItem; | |
} | |
- (NSInteger)numberOfChildItemsAtIndexPath:(NSIndexPath *)indexPath { | |
if (indexPath.length == 0) { | |
return 1; | |
} | |
if (indexPath.length == 1) { | |
switch ([indexPath indexAtPosition:0]) { | |
case 0: | |
return [self.channels count]; | |
} | |
} | |
return 0; | |
} | |
- (void)playableContentManager:(MPPlayableContentManager *)contentManager | |
initiatePlaybackOfContentItemAtIndexPath:(NSIndexPath *)indexPath | |
completionHandler:(void (^)(NSError *))completionHandler { | |
NSDictionary *channel = [[self channels] objectAtIndex:[indexPath indexAtPosition:1]]; | |
[self play:channel]; | |
completionHandler(nil); | |
} | |
-(void)play:(NSDictionary *)channel{ | |
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://stream.open.fm/%@", channel[@"id"]]]; | |
self.player = [[AVPlayer alloc]initWithURL:url]; | |
[self.player addObserver:self forKeyPath:@"status" options:0 context:nil]; | |
} | |
- (void)observeValueForKeyPath:(NSString *)keyPath | |
ofObject:(id)object | |
change:(NSDictionary *)change | |
context:(void *)context { | |
if (object == self.player && [keyPath isEqualToString:@"status"]) { | |
if (self.player.status == AVPlayerStatusFailed) { | |
NSLog(@"AVPlayer Failed"); | |
} else if (self.player.status == AVPlayerStatusReadyToPlay) { | |
NSLog(@"AVPlayerStatusReadyToPlay"); | |
[self.player play]; | |
} else if (self.player.status == AVPlayerItemStatusUnknown) { | |
NSLog(@"AVPlayer Unknown"); | |
} | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment