【问题标题】:React Native: Could not connect to development serverReact Native:无法连接到开发服务器
【发布时间】:2017-10-11 00:17:21
【问题描述】:

我有一个非常简单的 React Native 项目,我正在尝试开始工作。这是一个简单地将 RCTRootView 添加到 UIViewController 的 iOS 项目。当我从 Xcode 运行应用程序时,我得到一个带有错误的红屏:

Could not connect to development server.

Ensure the following:
- Node server is running and available on the same network - run 'npm start' from react-native root
- Node server URL is correctly set in AppDelegate

AppDelegate.h

@property (strong, nonatomic) RCTRootView *rootView;

AppDelegate.m in application: didFinishLaunchingWithOptions:

NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios"];
self.rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName:@"HelloReact" initialProperties:nil launchOptions:launchOptions];

ViewController.m in viewDidAppear:

AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[self.view addSubview:delegate.rootView];

我不知道如何解决这个问题。我已经尝试了以下所有方法:

  • npm 开始
  • npm start --reset-cache
  • 删除 node_modules 目录并重新安装
  • 卸载并重新安装 node、watchman、react-native 和 npm
  • 在物理设备和模拟器上试用

有没有人发现我的代码有任何问题或知道问题可能是什么?我愿意通过电子邮件或电话交谈来解决这个问题。我越来越绝望了。

我同时遇到了问题。这是我问的另一个问题,其中可能包含一些信息:React Native: Unable to Resolve Module

【问题讨论】:

  • 您是否在浏览器中打开了开发服务器选项卡?类似于http://localhost:8081/debugger-ui
  • 不,我没有打开那个。我从来没有读过这...你将不得不指出我正确的方向。不知道如何使用该工具。
  • 我无法让震动器工作并显示开发者菜单。我在物理设备上运行调试。我还需要做什么才能让它显示出来?
  • 太棒了,很高兴能帮上忙!

标签: ios objective-c node.js reactjs react-native


【解决方案1】:

确保您的防火墙没有干扰您。花了我们的时间试图弄清楚为什么这不起作用,我感到很愚蠢,这是我的防火墙??‍♂️

【讨论】:

    【解决方案2】:

    这是另一个潜在问题。您也在运行,这导致了这个问题。 expo 和 yarn 共享同一个端口,这也可能导致问题。我希望这会有所帮助!

    【讨论】:

      【解决方案3】:

      确保设备和 Macbook 在同一个 wifi 网络上,并使用 Lightning-USB 数据线插入。 See docs for more info

      【讨论】:

        【解决方案4】:
        1. 关闭自动打开的 Metro bundler 窗口命令行。
        2. 再次运行命令,“react-native run-android”或“react-native run-ios”。

        【讨论】:

          【解决方案5】:

          在 iOS 设备上运行 React Native 应用程序需要指定主机的 IP 地址而不是 localhost

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

          更具体的说明可以找到in this answer

          【讨论】:

            猜你喜欢
            • 2016-10-20
            • 2021-08-23
            • 1970-01-01
            • 2020-05-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多