首页 文章

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

提问于
浏览
2

我有一个非常简单的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;

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

1 回答

  • 1

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

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

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

相关问题