我正在使用react-native-carplay库开发一个支持CarPlay的React Native应用程序。当手机应用程序打开时,CarPlay 界面可以正常工作,但当手机应用程序关闭时,它无法加载 JavaScript。目前,CarPlay 在应用程序打开时也不会加载 JS,尽管这是更容易修复的问题。
我已经尝试遵守此README,尽管收效甚微。
// 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;
}
// Other necessary methods...
@end
// 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];
}
// Other necessary methods...
@end
// SceneDelegate.mm (redacted and consolidated)
@implementation SceneDelegate
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(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];
// Handle splash screen...
}
}
// Other necessary methods...
@end
// JavaScript
import { AppRegistry } from "react-native";
import CarCode from "./carplay/CarCode";
AppRegistry.registerComponent("CarPlayApp", () => CarCode);
我尝试过各种方法,包括:
尽管进行了这些尝试,当手机应用程序未运行时,CarPlay 界面仍保持空白。本机 iOS 代码似乎可以正常工作,但 JavaScript 并未加载或执行。 即使手机应用程序关闭,我还缺少什么来确保 React Native JavaScript 代码在 CarPlay 中运行? 附加信息:
React Native 版本:75.0 React-native-carplay 版本:2.4.1-beta.0 iOS版本:16.6
任何帮助或见解将不胜感激!