Sunday, December 15, 2024
HomeiOS Developmentios - React Native CarPlay app not loading JavaScript when telephone app...

ios – React Native CarPlay app not loading JavaScript when telephone app is closed


I am growing a React Native app with CarPlay help utilizing the react-native-carplay library. The CarPlay interface works accurately when the telephone app is open, nevertheless it fails to load the JavaScript when the telephone app is closed. At the moment, the CarPlay doesn’t load the JS when the app is open both, though that’s one thing simpler to repair.

I already tried adhering to this README, though to little avail.

// AppDelegate.mm (redacted and consolidated)
@implementation AppDelegate

- (BOOL)initAppFromScene:(UISceneConnectionOptions *)connectionOptions {
    if (self.rootView == nil) {
        self.rootViewFactory = [self createRCTRootViewFactory];
        NSDictionary *initProps = [self prepareInitialProps];
        self.rootView = [self.rootViewFactory viewWithModuleName:self.moduleName 
                                              initialProperties:initProps 
                                                  launchOptions:[self connectionOptionsToLaunchOptions:connectionOptions]];
        return YES;
    }
    return NO;
}

// Different needed strategies...

@finish


// CarSceneDelegate.mm (redacted and consolidated)
@implementation CarSceneDelegate

- (void)templateApplicationScene:(CPTemplateApplicationScene *)templateApplicationScene
    didConnectInterfaceController:(CPInterfaceController *)interfaceController {
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    [appDelegate initAppFromScene:nil];
    UIView *carPlayRootView = [appDelegate.rootViewFactory viewWithModuleName:@"CarPlayApp"
                                                            initialProperties:nil
                                                                launchOptions:nil];
    
    UIViewController *rootViewController = appDelegate.createRootViewController;
    [appDelegate setRootView:appDelegate.rootView toRootViewController:rootViewController];
    
    CPWindow *carWindow = templateApplicationScene.carWindow;
    carWindow.rootViewController = rootViewController;
    
    [carPlayRootView setFrame:carWindow.bounds];
    [carWindow addSubview:carPlayRootView];
  
    [RNCarPlay connectWithInterfaceController:interfaceController window:carWindow];
}

// Different needed strategies...

@finish

// SceneDelegate.mm (redacted and consolidated)
@implementation SceneDelegate

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session choices:(UISceneConnectionOptions *)connectionOptions
{
    if ([scene isKindOfClass:[UIWindowScene class]])
    {
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        BOOL hasCreatedBridge = [appDelegate initAppFromScene:connectionOptions];
        
        UIViewController *rootViewController = appDelegate.createRootViewController;
        [appDelegate setRootView:appDelegate.rootView toRootViewController:rootViewController];

        UIWindow *window = [[UIWindow alloc] initWithWindowScene:scene];
        window.rootViewController = rootViewController;
        self.window = window;
        appDelegate.window = window;
        [self.window makeKeyAndVisible];

        // Deal with splash display...
    }
}

// Different needed strategies...

@finish

// JavaScript
import { AppRegistry } from "react-native";
import CarCode from "./carplay/CarCode";

AppRegistry.registerComponent("CarPlayApp", () => CarCode);

I’ve tried varied approaches, together with:

  • Initializing the React Native bridge within the CarPlay scene delegate.
  • Making a separate root view for CarPlay.
  • Guaranteeing that the JavaScript bundle is loaded and executed.

Regardless of these makes an attempt, the CarPlay interface stays clean when the telephone app will not be operating. The native iOS code appears to be working, however the JavaScript will not be being loaded or executed.
What am I lacking to make sure that the React Native JavaScript code runs in CarPlay even when the telephone app is closed?
Extra info:

React Native model: 75.0
react-native-carplay model: 2.4.1-beta.0
iOS model: 16.6

Any assist or insights can be enormously appreciated!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments