Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native: Could not connect to development server

I have a very simple React Native project that I am trying to get working. It is an iOS project that simply adds a RCTRootView to a UIViewController. When I run the app from Xcode I get a red screen with the error:

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];

I am at a loss on how to resolve this. I have tried all of the following:

  • npm start
  • npm start --reset-cache
  • Deleted node_modules directory and reinstalled
  • Uninstalled and reinstalled node, watchman, react-native, and npm
  • Tried on physical device and simulator

Does anyone see anything that is wrong in my code or know what the issue might be? I'd be willing to converse over email or phone to solve this. I am getting desperate.

I was having simultaneous issues. Here is the other question I asked which may contain some info: React Native: Unable to Resolve Module

like image 423
tentmaking Avatar asked May 11 '17 20:05

tentmaking


People also ask

Can't connect to development server iOS react-native?

How to Solve Could not connect to development server Error in React Native : Step 1 : Cancel the current process of "react-native run-android" by CTRL + C or CMD + C. Step 2 :Close metro bundler window command line which opened automatically. Step 3 :Run the command again, "react-native run-android".

How do I start a development server in react-native?

Running your React Native application Install the Expo Go app on your iOS or Android phone and connect to the same wireless network as your computer. On Android, use the Expo Go app to scan the QR code from your terminal to open your project. On iOS, use the built-in QR code scanner of the default iOS Camera app.


1 Answers

Running a React Native app on an iOS device requires specifying the host machine's IP address instead of localhost:

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

More specific instructions can be found in this answer.

like image 167
NiFi Avatar answered Oct 08 '22 17:10

NiFi