React Native CarPlay 应用程序在手机应用程序关闭时不加载 JavaScript

问题描述 投票:0回答:1

我正在使用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 场景委托中初始化 React Native 桥。
  • 为 CarPlay 创建单独的根视图。
  • 确保 JavaScript 包已加载并执行。

尽管进行了这些尝试,当手机应用程序未运行时,CarPlay 界面仍保持空白。本机 iOS 代码似乎可以正常工作,但 JavaScript 并未加载或执行。 即使手机应用程序关闭,我还缺少什么来确保 React Native JavaScript 代码在 CarPlay 中运行? 附加信息:

React Native 版本:75.0 React-native-carplay 版本:2.4.1-beta.0 iOS版本:16.6

任何帮助或见解将不胜感激!

javascript ios objective-c react-native carplay
1个回答
0
投票

问题并非来自 iOS 代码。您面临的问题是因为 JAVASCRIPT。如果不知道,那么让我告诉你,JavaScript 不会在后台运行,因为 JavaScript 线程与 UI 线程相关联。

第二件事,iOS 会在后台暂停你的应用程序。

尝试提供支持在后台运行react-native(javascript)的库。

尝试背景定时器

后台任务

希望这对您有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.