tentmaking
tentmaking

Reputation: 2146

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:

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

Upvotes: 6

Views: 17758

Answers (5)

AP Fritts
AP Fritts

Reputation: 475

Make sure your firewall isn't messing with you. I feel so dumb for spending an our trying to figure out why this wasn't working, and it was my firewall 🤦🏻‍♂️

Upvotes: 0

Naman Jain
Naman Jain

Reputation: 361

Here is another potential issue. You are running as well which is causing this issue. expo and yarn share the same port and this can lead to issues as well. I hope this helps!

Upvotes: 0

CacheMeOutside
CacheMeOutside

Reputation: 737

Ensure that the device and Macbook are on the same wifi network and plugged in with the lightning-USB cable. See docs for more info

Upvotes: 0

Edicarlos Lopes
Edicarlos Lopes

Reputation: 1131

  1. Close metro bundler window command line which opened automatically.
  2. Run the command again, “react-native run-android” or "react-native run-ios".

Upvotes: 0

NiFi
NiFi

Reputation: 2458

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.

Upvotes: 5

Related Questions