meisel
meisel

Reputation: 2429

Can't connect react native to localhost

In my iOS app, which includes the React library and its dependencies, I have the following code on startup:

NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.bundle?platform=ios"]; RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName:@"App" initialProperties:@{} launchOptions:@{}]; rootView.frame = self.view.bounds; [self.view addSubview:rootView];

When I run this in the simulator, even after disabling ATS and running npm start, I get:

Failed to load bundle(http://localhost:8081/index.bundle?platform=ios) with 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 - WiFi is enabled and connected to the same network as the Node Server

My package.json file is this:

{ "name": "AwesomeProject", "version": "0.1.0", "private": true, "devDependencies": { "react-native-scripts": "1.11.1", "jest-expo": "25.0.0", "react-test-renderer": "16.2.0" }, "main": "./node_modules/react-native-scripts/build/bin/crna-entry.js", "scripts": { "start": "react-native-scripts start", "eject": "react-native-scripts eject", "android": "react-native-scripts android", "ios": "react-native-scripts ios", "test": "node node_modules/jest/bin/jest.js" }, "jest": { "preset": "jest-expo" }, "dependencies": { "expo": "^25.0.0", "react": "16.2.0", "react-native": "0.52.0", "react-navigation": "^1.5.11" } }

Another thing... it seems that npm start causes port 19000, not 8081, to open, but when I go to http://localhost:19000 and use the bundle URL that they provide in that JSON, it tells me that I need to use Expo. But none of that is mentioned in https://facebook.github.io/react-native/docs/integration-with-existing-apps.html, and I'm not sure where to go from here. Any ideas?

Upvotes: 0

Views: 2393

Answers (2)

Stackia
Stackia

Reputation: 2141

If you want to bring react-native to your existing native iOS project, then expo package isn't expected here, and the start script should be node node_modules/react-native/local-cli/cli.js start.

Just follow Integration with Existing Apps guide, you only need react-native and react package. (and react-navigation if your app use that for navigation).

Upvotes: 1

Kartik Shah
Kartik Shah

Reputation: 916

Comment this line in AppDelegate.m

 jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil]; 

Use this line:-

jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"]; 

After that run this to make jsbundle:-

 react-native bundle --entry-file index.js --platform ios --dev false --bundle-output ios/main.jsbundle --assets-dest ios

Run after this command in xcode . Hope it will make bundle and there is no such error after that ... Thanx

Upvotes: 0

Related Questions