xcode – React Native – 无法执行JS调用:__ fbBatchedBridge未定义

我一直在关注这个教程
https://www.raywenderlich.com/126063/react-native-tutorial

并决定在遇到问题后从头开始.

我运行了react-native init PropertyFinder并在Xcode中打开了项目.当我编译并运行它时,它会在模拟器中按预期打开显示:

《xcode – React Native – 无法执行JS调用:__ fbBatchedBridge未定义》

但不久之后屏幕消失并显示:

《xcode – React Native – 无法执行JS调用:__ fbBatchedBridge未定义》

错误文本是:

Unable to execute JS call: __fbBatchedBridge is undefined

它工作不到24小时,所以不确定发生了什么. Fwiw,我完全删除了该项目并重新开始.

这个答案(Unable to execute JS call: __fbBatchedBridge is undefined)建议检查它是否通过网络获取代码.这似乎不是问题.

App Delegate中的完整代码如下:

/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

#import "AppDelegate.h"

#import "RCTRootView.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  NSURL *jsCodeLocation;

  /**
   * Loading JavaScript code - uncomment the one you want.
   *
   * OPTION 1
   * Load from development server. Start the server from the repository root:
   *
   * $npm start
   *
   * To run on device, change `localhost` to the IP address of your computer
   * (you can get this by typing `ifconfig` into the terminal and selecting the
   * `inet` value under `en0:`) and make sure your computer and iOS device are
   * on the same Wi-Fi network.
   */

  jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];

  /**
   * OPTION 2
   * Load from pre-bundled file on disk. The static bundle is automatically
   * generated by the "Bundle React Native code and images" build step when
   * running the project on an actual device or running the project on the
   * simulator in the "Release" build configuration.
   */

//   jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];

  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"PropertyFinder"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  return YES;
}

@end

最佳答案 这很可能是由于App尝试从捆绑包而不是包装器加载.您只能通过打包程序运行模拟器.转到AppDelegate.m文件并确保取消注释此行

jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];

这条线被评论

// jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];

该应用程序仍然会加载,因为它只是加载屏幕,这是React之前.一旦React启动它就会失败.

This thread talks about it in more detail.

点赞