[英]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: 在我的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];
When I run this in the simulator, even after disabling ATS and running npm start
, I get: 当我在模拟器中运行此程序时,即使在禁用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
My package.json
file is this: 我的
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" } }
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. 另一件事...
npm start
似乎导致打开端口19000(而不是8081),但是当我转到http:// localhost:19000并使用它们在JSON中提供的捆绑包URL时,它告诉我我需要使用世博会。 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. 但是在https://facebook.github.io/react-native/docs/integration-with-existing-apps.html中没有提到任何内容,而且我不确定从何处去。 Any ideas?
有任何想法吗?
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
. 如果您想将react-native引入您现有的本地iOS项目中,则这里不需要
expo
包,并且开始脚本应为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. 只需遵循与现有应用程序集成指南,您只需要
react-native
和react
软件包即可。 (and react-navigation
if your app use that for navigation). (和
react-navigation
,如果您的应用程序使用导航)。
Comment this line in AppDelegate.m 在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:- 之后,运行此命令来制作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 . 在xcode中此命令之后运行。 Hope it will make bundle and there is no such error after that ... Thanx
希望它将使捆绑,并且在那之后没有这种错误... Thanx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.