ios - 无法连接 native 到本地主机

标签 ios react-native

在我的 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-nativereact 包。 (以及 react-navigation 如果您的应用使用它进行导航)。

关于ios - 无法连接 native 到本地主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49788013/

相关文章:

react-native - 不变违规 : Element type is invalid: expected a string (for built-in components) or a class/function but got: undefined

ios - 像 Tinder 一样自定义 UISegmentedControl - Swift 3

ios - 快速显示和隐藏 CALayer 的速度似乎很慢

android - 如何在 FirebaseAnalytics for Unity3D 中设置 userId?

ios - 博览会:没有为具有捆绑标识符的应用程序配置凭据

react-native - 单击时如何在键盘上方设置文本输入? react 原生

iphone - mobile safari 如何处理 pdf 以便提供搜索功能?

c# - Xamarin iOS Sleep UI线程不起作用

react-native - 如何在具有 TabNavigator 的应用程序的根级别显示模式?

xcode - 在 React Native iOS 应用程序中更改端口