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

ten*_*ing 6 objective-c node.js ios 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
Run Code Online (Sandbox Code Playgroud)

AppDelegate.h

@property (strong, nonatomic) RCTRootView *rootView;
Run Code Online (Sandbox Code Playgroud)

AppDelegate.m 中 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];
Run Code Online (Sandbox Code Playgroud)

ViewController.m 中 viewDidAppear:

AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[self.view addSubview:delegate.rootView];
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

NiF*_*iFi 5

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

jsCodeLocation = [NSURL URLWithString:@"http://DEV_SERVER_URI:8081/index.ios.bundle?platform=ios&dev=true"];
Run Code Online (Sandbox Code Playgroud)

更具体的说明可以在这个答案中找到。