React Native:无法连接到开发服务器

时间:2017-05-11 20:18:22

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

我有一个非常简单的React Native项目,我正在努力工作。这是一个iOS项目,它只是将一个RCTRootView添加到UIViewController中。当我从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;

application: didFinishLaunchingWithOptions:

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

viewDidAppear:

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

我对如何解决这个问题感到茫然。我尝试了以下所有方法:

  • npm start
  • npm start --reset-cache
  • 删除了node_modules目录并重新安装
  • 卸载并重新安装了node,watchman,react-native和npm
  • 尝试使用物理设备和模拟器

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

我有同时发生的问题。这是我提出的另一个问题,其中可能包含一些信息:React Native: Unable to Resolve Module

5 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

  1. 关闭都会自动打开的Metro bundler窗口命令行。
  2. 再次运行命令“ react-native run-android”或“ react-native run-ios”。

答案 2 :(得分:0)

确保设备和Macbook在同一个wifi网络上,并已通过闪电USB电缆插入。 See docs for more info

答案 3 :(得分:0)

这是另一个潜在问题。您也正在运行,这是导致此问题的原因。博览会和纱线共用一个端口,这也可能导致问题。我希望这会有所帮助!

答案 4 :(得分:0)

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

相关问题