What if you could create CarPlay with React Native. Well, now you can.
- Install the library
yarn add react-native-carplay --save
- Link using normal or cocoapods method
react-native link react-native-carplay
# in ios/Podfile:
pod 'react-native-carplay', path: '../node_modules/react-native-carplay'
- Edit your AppDelegate
// AppDelegate.h
// [step 1] add this line to the top
#import <CarPlay/CarPlay.h>
// [step 2] add the "CPApplicationDelegate" to the end, before ">":
@interface AppDelegate : UIResponder <UIApplicationDelegate, CPApplicationDelegate>
// AppDelegate.m
// [step 1] add this line to the top
#import <RNCarPlay.h>
// ...
// [step 2] add the following two methods before @end
- (void)application:(UIApplication *)application didConnectCarInterfaceController:(CPInterfaceController *)interfaceController toWindow:(CPWindow *)window {
[RNCarPlay connectWithInterfaceController:interfaceController window:window];
}
- (void)application:(nonnull UIApplication *)application didDisconnectCarInterfaceController:(nonnull CPInterfaceController *)interfaceController fromWindow:(nonnull CPWindow *)window {
[RNCarPlay disconnect];
}
@end
import { CarPlay, GridTemplate } from 'react-native-carplay';
const template = new GridTemplate();
CarPlay.setRootTemplate(template);
Sets the root template of CarPlay.
This must be called before running any other CarPlay commands. Can be called multiple times.
CarPlay.setRootTemplate(template, /* animated */ false);
Pushes a new template to the navigation stack.
Note you cannot push the same template twice.
(where template
is one of GridTemplate, ListTemplate or SearchTemplate)
CarPlay.pushTemplate(template, /* animated */ true);
Pop currently presented template from the stack.
CarPlay.popTemplate(/* animated */ false);
Pop currently presented template from the stack to a specific template. The template must be in the stack.
CarPlay.popToTemplate(template, /* animated */ false);
Pop the stack to root template.
CarPlay.popToRoot(/* animated */ false);
import { CarPlay, MapTemplate } from 'react-native-carplay';
function CarPlayView() {
return (
<View>
<Image style={{ width: 100, height: 100 }} />
<Text>My thing</Text>
<GoogleMap />
</View>
);
}
const map = new MapTemplate({
guidanceBackgroundColor: '#aeafaf',
component: CarPlayView,
mapButtons: [{
id: 'test',
image: require('assets/images/test.png'),
focusedImage: require('assets/images/test-focused.png'),
}]
});
CarPlay.setRootTemplate(map);
import { CarPlay, ListTemplate } from 'react-native-carplay';
// Register a new template in memory
const artists = new ListTemplate({
title: 'List of artists',
leadingNavigationBarButtons: [{
id: 'play',
type: 'text',
title: 'Play',
}],
sections: [{
items: [{
text: 'AC/DC'
detailText: 'Rock',
image: require('./artists/ac-dc.png'),
showsDisclosureIndicator: true,
}],
}],
onItemSelect(item) {
const artist = new ListTemplate({
title: 'AC/DC',
sections: [...],
});
CarPlay.pushTemplate(artist, true);
}
});
CarPlay.setRootTemplate(songs, false);
- CPListTemplate
- CPGridTemplate
- CPSearchTemplate
- CPMapTemplate
- CPVoiceControlTemplate
- CPAlertTemplate
- CPActionSheetTemplate
- CPNavigationSession
- CPTrip
- CPManeuver
- CPAlertAction
- CPSessionConfiguration
- setRootTemplate
- pushTemplate
- popTemplate
- popToTemplate
- presentTemplate
- dismissTemplate
- updateListTemplateSections
- reactToUpdatedSearchText
- reactToSelectedResult
- topTemplate
- rootTemplate
- didConnect
- didDisconnect
- didSelectListItem
- selectedResult
- gridButtonPressed
- updatedSearchText
- searchButtonPressed
- barButtonPressed