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

时间:2016-06-17 08:50:33

标签: xcode react-native

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

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

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

enter image description here

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

enter image description here

错误文字是:

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

4 个答案:

答案 0 :(得分:9)

重新启动地铁捆绑器对我有帮助!

答案 1 :(得分:2)

这很可能是由于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

答案 2 :(得分:0)

您可以尝试此更改

jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/?platform=ios&dev=false"];

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

在文件AppDelegate.m中。 重点是" index.ios.bundle"有时它是" index.ios"然后添加" .bundle"在它之后。我用这个解决了这个问题。

答案 3 :(得分:0)

请检查link.

这个解决方案解决了我的问题。