Skip to content

Commit f16de4d

Browse files
authored
docs: update CarPlay support on example app for latest RN versions (#510)
1 parent e04d42a commit f16de4d

File tree

6 files changed

+47
-11
lines changed

6 files changed

+47
-11
lines changed

CARPLAY.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ This guide explains how to enable and integrate Apple CarPlay with the React Nat
1212

1313
Refer to the [Apple CarPlay Developer Guide](https://developer.apple.com/carplay/) to understand how CarPlay works and to complete the initial setup. Key steps include:
1414

15-
- Adding the CarPlay entitlement to your Xcode project.
16-
- Creating a separate scene for the CarPlay map and enabling support for multiple scenes.
15+
- Adding the CarPlay entitlement to your Xcode project
16+
- Creating a separate scene for the CarPlay map and enabling support for multiple scenes
17+
18+
For a complete implementation example, refer to [AppDelegateCarPlay.m](example/ios/SampleApp/AppDelegateCarPlay.m), [PhoneSceneDelegate.m](example/ios/SampleApp/PhoneSceneDelegate.m) and [CarSceneDelegate.m](example/ios/SampleApp/CarSceneDelegate.m). Pay special attention to how the React Native window is initialized in `PhoneSceneDelegate` when using multiple scenes.
1719

1820
### SceneDelegate for CarPlay
1921

@@ -83,4 +85,10 @@ For a more detailed example, refer to the `NavigationScreen.tsx` in the React Na
8385

8486
## Example Project
8587

86-
For a fully functional CarPlay implementation, check out the [SampleApp](./example/ios/) Xcode project, which includes the `SampleAppCarPlay` build target. The sample already contains test entitlement so you don't need to request one from Apple to run it.
88+
For a fully functional CarPlay implementation, check out the [SampleApp](./example/ios/) Xcode project, which includes the `SampleAppCarPlay` build target. The sample already contains test entitlement so you don't need to request one from Apple to run it.
89+
90+
Start the CarPlay example from the command line:
91+
92+
```bash
93+
yarn example ios:carplay
94+
```

example/ios/SampleApp.xcodeproj/project.pbxproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,10 @@
643643
ENABLE_BITCODE = NO;
644644
INFOPLIST_FILE = "SampleApp/Info-CarPlay.plist";
645645
IPHONEOS_DEPLOYMENT_TARGET = 16.6;
646+
GCC_PREPROCESSOR_DEFINITIONS = (
647+
"$(inherited)",
648+
"CARPLAY=1",
649+
);
646650
LD_RUNPATH_SEARCH_PATHS = (
647651
"$(inherited)",
648652
"@executable_path/Frameworks",
@@ -670,8 +674,12 @@
670674
CLANG_ENABLE_MODULES = YES;
671675
CODE_SIGN_ENTITLEMENTS = SampleApp/SampleApp.entitlements;
672676
CURRENT_PROJECT_VERSION = 1;
673-
INFOPLIST_FILE = SampleApp/Info.plist;
677+
INFOPLIST_FILE = "SampleApp/Info-CarPlay.plist";
674678
IPHONEOS_DEPLOYMENT_TARGET = 16.6;
679+
GCC_PREPROCESSOR_DEFINITIONS = (
680+
"$(inherited)",
681+
"CARPLAY=1",
682+
);
675683
LD_RUNPATH_SEARCH_PATHS = (
676684
"$(inherited)",
677685
"@executable_path/Frameworks",

example/ios/SampleApp/AppDelegateCarPlay.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,4 @@
1717
#import <UIKit/UIKit.h>
1818

1919
@interface AppDelegateCarPlay : RCTAppDelegate
20-
@property(nonatomic, strong) UIWindow *window;
21-
@property(nonatomic, strong) RCTBridge *bridge;
22-
@property(nonatomic, strong) RCTRootView *rootView;
2320
@end

example/ios/SampleApp/AppDelegateCarPlay.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ - (BOOL)application:(UIApplication *)application
3030
self.moduleName = @"SampleApp";
3131
self.dependencyProvider = [RCTAppDependencyProvider new];
3232

33+
// You can add your custom initial props in the dictionary below.
34+
// They will be passed down to the ViewController used by React Native.
35+
self.initialProps = @{};
36+
3337
// Note: Ensure that you have copied the Keys.plist.sample to Keys.plist
3438
// and have added the correct API_KEY value to the file.
3539
//
@@ -39,6 +43,11 @@ - (BOOL)application:(UIApplication *)application
3943
NSString *api_key = [keysDictionary objectForKey:@"API_KEY"];
4044

4145
[GMSServices provideAPIKey:api_key];
46+
47+
// Set automaticallyLoadReactNativeWindow to NO to prevent RCTAppDelegate from creating the
48+
// window. It will be created in PhoneSceneDelegate for the main (phone) screen.
49+
self.automaticallyLoadReactNativeWindow = NO;
50+
4251
return [super application:application didFinishLaunchingWithOptions:launchOptions];
4352
}
4453

@@ -65,6 +74,14 @@ - (void)application:(UIApplication *)application
6574
didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
6675
}
6776

77+
- (BOOL)fabricEnabled {
78+
return NO;
79+
}
80+
81+
- (BOOL)bridgelessEnabled {
82+
return NO;
83+
}
84+
6885
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
6986
return [self bundleURL];
7087
}

example/ios/SampleApp/PhoneSceneDelegate.m

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,16 @@
1414
* limitations under the License.
1515
*/
1616
#import "PhoneSceneDelegate.h"
17+
#import <RCTAppDelegate.h>
1718
#import <React/RCTRootView.h>
1819
#import <UIKit/UIKit.h>
19-
#import "AppDelegateCarPlay.h"
2020

2121
@implementation PhoneSceneDelegate
2222

2323
- (void)scene:(UIScene *)scene
2424
willConnectToSession:(UISceneSession *)session
2525
options:(UISceneConnectionOptions *)connectionOptions {
26-
AppDelegateCarPlay *appDelegate =
27-
(AppDelegateCarPlay *)[UIApplication sharedApplication].delegate;
26+
RCTAppDelegate *appDelegate = (RCTAppDelegate *)[UIApplication sharedApplication].delegate;
2827
if (!appDelegate) {
2928
return;
3029
}
@@ -34,12 +33,18 @@ - (void)scene:(UIScene *)scene
3433
return;
3534
}
3635

36+
UIView *rootView = [appDelegate.rootViewFactory viewWithModuleName:appDelegate.moduleName
37+
initialProperties:appDelegate.initialProps
38+
launchOptions:nil];
39+
rootView.backgroundColor = [UIColor whiteColor];
40+
3741
UIViewController *rootViewController = [[UIViewController alloc] init];
38-
rootViewController.view = appDelegate.rootView;
42+
rootViewController.view = rootView;
3943

4044
UIWindow *window = [[UIWindow alloc] initWithWindowScene:windowScene];
4145
window.rootViewController = rootViewController;
4246
self.window = window;
47+
4348
[appDelegate setWindow:window];
4449
[window makeKeyAndVisible];
4550
}

example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"android": "react-native run-android",
88
"android-release": "react-native run-android --mode release",
99
"ios": "react-native run-ios",
10+
"ios:carplay": "react-native run-ios --scheme SampleAppCarPlay",
1011
"ios-release": "react-native run-ios --mode Release",
1112
"lint": "eslint .",
1213
"test": "jest",

0 commit comments

Comments
 (0)