无法连接本地响应localhost

时间:2018-04-12 04:29:22

标签: ios react-native

在我的iOS应用程序中,包括React库及其依赖项,我在启动时有以下代码:

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

当我在模拟器中运行它时,即使禁用ATS并运行npm start,我也会得到:

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

我的package.json文件是这样的:

{ "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" } }

另一件事......似乎npm start导致端口19000而不是8081,但是当我转到http://localhost:19000并使用它们在JSON中提供的包URL时,它会告诉我需要使用世博会。但https://facebook.github.io/react-native/docs/integration-with-existing-apps.html中没有提到这一点,我不知道从哪里开始。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

如果您想为现有的原生iOS项目提供本地反应,那么此处不需要expo包,启动脚本应为node node_modules/react-native/local-cli/cli.js start

只需按照Integration with Existing Apps指南操作,您只需要react-nativereact个包。 (以及react-navigation如果您的应用将其用于导航)。

答案 1 :(得分:0)

在AppDelegate.m中注释这一行

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

使用此行: -

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

之后运行此命令以生成jsbundle: -

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

在xcode中运行此命令。希望它会捆绑,之后没有这样的错误...... 感谢名单