【问题标题】:Can't connect react native to localhost无法将本机反应连接到本地主机
【发布时间】:2018-04-12 04:29:22
【问题描述】:

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

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 中没有提到这些,我不确定从这里去哪里。有什么想法吗?

【问题讨论】:

  • 你能发布你的package.json吗?
  • @Stackia 发布

标签: ios react-native


【解决方案1】:

如果你想将 react-native 引入你现有的原生 iOS 项目,那么这里不需要 expo 包,启动脚本应该是 node node_modules/react-native/local-cli/cli.js start

只需按照Integration with Existing Apps 指导,您只需要react-nativereact 包。 (以及react-navigation,如果您的应用使用它进行导航)。

【讨论】:

    【解决方案2】:

    在 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 中执行此命令后运行。希望它会捆绑并且之后没有这样的错误...... 谢谢

    【讨论】:

    • 我在哪里可以得到这个react-native 可执行文件?
    • 啊,我找到了
    • 解决了没有??
    猜你喜欢
    • 2014-11-06
    • 2012-03-22
    • 2016-01-01
    • 2021-06-20
    • 2015-07-26
    • 2015-11-04
    • 2016-04-25
    • 2015-12-25
    相关资源
    最近更新 更多