在我的 iOS 应用程序(包含 React 库及其依赖项)中,我在启动时有以下代码:
NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.bundle?platform=ios"];
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
模块名称:@“应用程序”
初始属性:@{}
启动选项:@{}];
rootView.frame = self.view.bounds;
[self.view addSubview:rootView];
当我在模拟器中运行它时,即使在禁用 ATS 并运行 npm start
之后,我得到:
无法加载包 (http://localhost:8081/index.bundle?platform=ios),出现错误:(无法连接到开发服务器。
确保以下几点:
- 节点服务器正在运行并在同一网络上可用 - 从 react-native root 运行“npm start”
- 在 AppDelegate 中正确设置了节点服务器 URL
- WiFi 已启用并连接到与节点服务器相同的网络
我的 package.json
文件是这样的:
{
“名称”:“很棒的项目”,
“版本”:“0.1.0”,
“私有(private)”:真实,
“devDependencies”:{
“ react native 脚本”:“1.11.1”,
"玩笑博览会": "25.0.0",
“ react 测试渲染器”:“16.2.0”
},
“主要”:“./node_modules/react-native-scripts/build/bin/crna-entry.js”,
“脚本”:{
"start": "react-native-scripts 开始",
"eject": "react-native-scripts 弹出",
"android": "react-native-scripts android",
"ios": "react-native-scripts ios",
“测试”:“节点node_modules/jest/bin/jest.js”
},
“开玩笑”:{
“预设”:“玩笑博览会”
},
“依赖”:{
"博览会": "^25.0.0",
“ react ”:“16.2.0”,
“ react native ”:“0.52.0”,
“ react 导航”:“^1.5.11”
}
}
另一件事...似乎 npm start
导致端口 19000 而不是 8081 打开,但是当我转到 http://localhost:19000 时并使用他们在该 JSON 中提供的捆绑 URL,它告诉我我需要使用 Expo。但https://facebook.github.io/react-native/docs/integration-with-existing-apps.html中没有提到这些。 ,我不知道从这里去哪里。有什么想法吗?
最佳答案
如果你想将react-native引入你现有的原生iOS项目,那么这里不需要expo
包,启动脚本应该是node node_modules/react-native/local-cli/cli.js 启动
。
关注Integration with Existing Apps即可指南,你只需要 react-native
和 react
包。 (以及 react-navigation
如果您的应用使用它进行导航)。
关于ios - 无法连接 native 到本地主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49788013/