xcode 使用设备上的预捆绑文件 React Native,即使是 DEV 模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41713678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:43:55  来源:igfitidea点击:

React Native using pre-bundled file on device, even if DEV-mode

iosxcodereact-native

提问by Viktor Nilsson

A few days ago, when I build and run my react native app from Xcode on a device, I realised that even if I set my scheme to Debug, the app is still loading from a pre-bundled file. Other than that, everything else is like developer-mode. I can shake the device and the developer meny pop ups, in the console I get output, and react native also logs:

几天前,当我在设备上从 Xcode 构建并运行我的 React Native 应用程序时,我意识到即使我将我的方案设置为调试,该应用程序仍在从预捆绑的文件中加载。除此之外,其他一切都像开发人员模式。我可以摇动设备,开发人员会弹出窗口,在控制台中我得到输出,并且反应原生也记录:

Running application "XXXXX" with appParams: {"rootTag":1,"initialProps":{}}. DEV=== true, development-level warning are ON, performance optimizations are OFF

使用 appParams 运行应用程序“XXXXX”:{"rootTag":1,"initialProps":{}}。DEV=== true,开发级警告开启,性能优化关闭

So when I try to reload from the developer meny, it indeed reloads but with the message

因此,当我尝试从开发人员meny 重新加载时,它确实重新加载但带有消息

Loading from pre-bundled file

从预捆绑文件加载

I have tried to delete the app, verify Debug-scheme, clean build, and the build and run again, without any different outcome.

我试图删除应用程序,验证调试方案,清理构建,然后构建并再次运行,没有任何不同的结果。

Does anyone have a clue what could have caused this (I have been running the app before with Release Scheme, but never have had this issue), and how I can get back to "normal"?

有没有人知道是什么导致了这种情况(我之前一直在使用 Release Scheme 运行该应用程序,但从未遇到过这个问题),以及如何恢复“正常”?

Unfortunately I do not know how to reproduce this.

不幸的是,我不知道如何重现这一点。

  • Xcode: 8.2.1
  • React-native: 0.40.0
  • 代码:8.2.1
  • 反应原生:0.40.0

Thank you for your time!

感谢您的时间!

Solved

解决了

In my case, it was as @while1 pointed out regarding the wifi. My device and computer where not connected to the same wifi. I however found a network at my working place where it still didn't work, even if both computer and device connected to this network (protected somehow maybe?). ANyway, when I shared my internet connection to my computer from my iPhone, the app finally loaded the data from my computer. So it indeed was the iPhone not being able to connect to my computer, and therefore loading a pre-bundled file.

就我而言,正如@while1 指出的有关 wifi 的问题。我的设备和电脑没有连接到同一个 wifi。然而,我在我的工作场所发现了一个网络,但它仍然无法工作,即使计算机和设备都连接到该网络(可能以某种方式受到保护?)。无论如何,当我从我的 iPhone 与我的计算机共享我的互联网连接时,该应用程序终于从我的计算机加载了数据。所以确实是 iPhone 无法连接到我的电脑,因此加载了一个预先捆绑的文件。

采纳答案by while1

In react native 0.40.0, React native try to guess bundle location by itself. Please see if following points can help:

在 react native 0.40.0 中,React native 尝试自行猜测包的位置。请看看以下几点是否有帮助:

  • Make sure your AppDelegate.m have this line:jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];

  • Check your device and your Xcode mac is running on same wifi. Internally for IOS, react native try to guess ip by itself.

  • Try running app first through command line, so dev packager server is running and then run app through xcode in your device.

  • 确保您的AppDelegate.m这一行:jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];

  • 检查您的设备和您的 Xcode mac 是否在同一 wifi 上运行。在 IOS 内部,react native 尝试自行猜测 ip。

  • 尝试首先通过命令行运行应用程序,以便开发包服务器正在运行,然后在您的设备中通过 xcode 运行应用程序。

回答by Codesingh

so your problem is here:

所以你的问题在这里:

Change code in your AppDelegate.m:

更改 AppDelegate.m 中的代码:

jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"]; // comment this line

jsCodeLocation = [NSURL URLWithString:@"http://xxx.xxx.x.xx:8081/index.ios.bundle?platform=ios&dev=true"]; //uncomment this line  

Cheers:)

干杯:)

回答by Krishna Mudhiraj

As i have faced same problem and above points didn't work for me. i got it worked by doing these steps.

因为我遇到了同样的问题,以上几点对我不起作用。我通过执行这些步骤使其工作。

  • Go to xcode-> your project-> info.plist
  • In "App Transport Security settings" make "Allow Arbitrary Loads" Yes.
  • 转到 xcode-> 您的项目-> info.plist
  • 在“应用程序传输安全设置”中,将“允许任意加载”设为“是”。

it should fix this issue.

它应该解决这个问题。

回答by Honza Kalfus

For those who are connected to the same Wi-Fi, but to a different IP address than localhost (you've set it up in RCTWebSocketExecutor.m):

对于那些连接到同一个 Wi-Fi,但连接到与本地主机不同的 IP 地址的用户(您已在 中设置RCTWebSocketExecutor.m):

Check Xcode debug console, if you can find the following error:

检查 Xcode 调试控制台,如果可以发现以下错误:

App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
  1. Use the following answer: https://stackoverflow.com/a/32704702/3979621

  2. And then use Codesingh's answer. BTW I recommend using the #ifdef DEBUGdirective as follows, so that you don't have to change it manually when switching between build variants:

  1. 使用以下答案:https: //stackoverflow.com/a/32704702/3979621

  2. 然后使用 Codesingh 的答案。顺便说一句,我建议#ifdef DEBUG按如下方式使用该指令,以便在构建变体之间切换时不必手动更改它:

.

.

#ifdef DEBUG
  jsCodeLocation = [NSURL URLWithString:@"http://xxx.xxx.xxx.xxx:8081/index.ios.bundle?platform=ios&dev=true"];
#else
  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
#endif

回答by Benni

in my case it was a (for react native) unexpected networksetting, so it grepped the wrong ip.

就我而言,这是一个(对于本机反应)意外的网络设置,因此它抓取了错误的 IP。

Check the Build report if your ip is correctly detected

如果您的 ip 被正确检测到,请检查 Build 报告

react native docs

反应原生文档

回答by Stian Jensen

For me, I had accidentally clicked Deny instead of Allow on a macos popup asking whether to accept incoming network connections to ?node?.

对我来说,我不小心在 macos 弹出窗口中单击了拒绝而不是允许,询问是否接受传入的网络连接到?节点?。

Opening System Preferences -> Security & Privacy -> Firewall -> Firewall options, and finding the line for node that was set to block, then correcting it to allow solved my problem!

打开系统偏好设置 -> 安全和隐私 -> 防火墙 -> 防火墙选项,找到设置为阻止的节点行,然后更正它以允许解决我的问题!

回答by Myalakin

For me, this happened after updating my mac to Mojave. Turns out that with the update the firewall settings changed to block all incoming connections... Thank you Stian Jensen for getting me on the track of checking my firewall!

对我来说,这是在将我的 mac 更新到 Mojave 之后发生的。事实证明,随着更新,防火墙设置更改为阻止所有传入连接......感谢 Stian Jensen 让我开始检查我的防火墙!

What I did:

我做了什么:

  1. Open System Preferences -> Security & Privacy -> Firewall -> Firewall options
  2. Uncheck "Block all incoming connections"
  3. Make sure that the connections listed are from sources that you trust
  1. 打开系统偏好设置 -> 安全与隐私 -> 防火墙 -> 防火墙选项
  2. 取消选中“阻止所有传入连接”
  3. 确保列出的连接来自您信任的来源