ios React 本机应用程序卡在设备的启动屏幕上,但可在模拟器中运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42346615/
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-08-31 08:55:04  来源:igfitidea点击:

React native app stuck on splash screen on device but works in simulator

iosxcodereact-native

提问by Thomas Charlesworth

My React Native app works in the XCode simulator with no issues, but when I run in a physical device, my iPhone, there's a problem. The app launches and gets stuck on the React Native splash screen, the after 10-15 seconds the app crashes/closes. What's the cause of this and how can I prevent it?

我的 React Native 应用程序在 XCode 模拟器中运行没有问题,但是当我在物理设备上运行时,我的 iPhone 出现了问题。应用程序启动并卡在 React Native 闪屏上,10-15 秒后应用程序崩溃/关闭。这是什么原因造成的,我该如何预防?

回答by Guannan He

It seems that I have found the problem. According to https://facebook.github.io/react-native/docs/running-on-device, when you build and run the app on your device, your app will load js files from the packager on your computer, so you can live reload your app. That means your device has to be connected to your computer or has to be in the same wifi network as your computer. If your device can't access the packager, it will stuck on the splash screen and quit.

看来我已经找到了问题所在。根据https://facebook.github.io/react-native/docs/running-on-device,当您在您的设备上构建和运行应用程序时,您的应用程序将从您计算机上的打包程序加载 js 文件,因此您可以实时重新加载您的应用程序。这意味着您的设备必须连接到您的计算机或必须与您的计算机处于同一 wifi 网络中。如果您的设备无法访问打包程序,它将卡在启动屏幕上并退出。

To run on your device reliably, edit build schema and build release version.

要在您的设备上可靠地运行,请编辑构建架构和构建发布版本。

回答by Freewalker

It should be perfectly possible to run the app in debug, on the device, without the packager attached! You have to use react-native bundleto create an offline bundle, and add it to your Xcode project. Then your app should fall back to that bundle when the packager is not available.

应该完全有可能在设备上调试运行应用程序,而无需附加打包程序!您必须使用react-native bundle来创建一个离线包,并将其添加到您的 Xcode 项目中。然后,当打包程序不可用时,您的应用程序应该回退到该捆绑包。

This used to be in the Deploying to Device FB docs, not sure why it's not there anymore.

这曾经在部署到设备 FB 文档中,不知道为什么它不再存在。

Sample call (our index.ios.jsis placed in ./distby TypeScript):

示例调用(我们index.ios.js./distTypeScript放入):

react-native bundle --dev true --assets-dest ./ios --entry-file ./dist/index.ios.js --platform ios --bundle-output ios/main.jsbundle

Also, it's apparently necessary to tell your app to run directly from the bundle rather than try to access the development server, which seems to cause the timeout (we had the same issue as OP).

此外,显然有必要告诉您的应用程序直接从包中运行,而不是尝试访问开发服务器,这似乎会导致超时(我们遇到了与 OP 相同的问题)。

Comment out this line:

注释掉这一行:

jsCodeLocation = // whatever

And add this line:

并添加这一行:

jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];

回答by Anjum....

For me when I build, it was working fine in simulator but in actual device only splash screen was coming and nothing else.

对我来说,当我构建时,它在模拟器中运行良好,但在实际设备中,只有闪屏出现,没有别的。

This was because my build configuration was debug mode which is default I guess, I had to change release/build configuration from debug to release and everything work as expected.

这是因为我的构建配置是调试模式,这是我猜的默认模式,我不得不将发布/构建配置从调试更改为发布,并且一切都按预期工作。

enter image description here

在此处输入图片说明

回答by Guy

I tried disconnecting my iPhone from the Internet, the problem disappeared in later launches.

我尝试断开我的 iPhone 与互联网的连接,问题在以后的发布中消失了。

So something is trying to do network stuff and causing the delay. It's a quick fix, but will do until we find the specific culprit.

所以有些东西试图做网络事情并导致延迟。这是一个快速修复,但在我们找到具体的罪魁祸首之前会这样做。

回答by Udaya Sri

I was having the same issue.
What I did was in Xcode go to Products -> Scheme -> Edit Scheme -> Select the Run tab -> Change the build configuration to Release ( default it was debug mode )and run the app in the device.

我遇到了同样的问题。
我所做的是在 Xcode 中转到Products -> Scheme -> Edit Scheme -> 选择 Run 选项卡 -> 将构建配置更改为 Release(默认为调试模式)并在设备中运行应用程序。

It was much faster and run it as like a native app.

它要快得多,并且像本机应用程序一样运行它。

回答by dayitv89

This issue could raise due to the following possibilities:

由于以下可能性,可能会出现此问题:

  1. Your run schema set to releaseinstead of debug, so always expect to load main.jsbundle, instead of running from the debug server. Change it by Product->Schema-> Edit Schema->Run-> Build Configuration: Debug
  1. 您的运行模式设置为release而不是debug,因此始终期望加载main.jsbundle,而不是从调试服务器运行。通过Product->Schema-> Edit Schema->Run-> Build Configuration: Debug更改它

Check on Xcode log that any message like NSURLConnection finished with error - code -1004or NSURLConnection finished with error - code -1022.Go to Project->Target->Build Phases-> + -> New Run Script Phase

检查 Xcode 日志中是否有任何类似NSURLConnection finished with error - code -1004或 的消息NSURLConnection finished with error - code -1022转到 Project->Target->Build Phases-> + -> New Run Script Phase

  1. (-1004) You have configured your XCode project from sketch and forgot to enter the react-native xcode script on Build Phases:
  1. (-1004) 您已经从草图配置了您的 XCode 项目,但忘记在构建阶段输入 react-native xcode 脚本:

For the below version of React Native 50: export NODE_BINARY=node ../node_modules/react-native/packager/react-native-xcode.sh

对于以下版本的 React Native 50: export NODE_BINARY=node ../node_modules/react-native/packager/react-native-xcode.sh

For the higher version of React Native 50: export NODE_BINARY=node ../node_modules/react-native/scripts/react-native-xcode.sh

对于更高版本的 React Native 50: export NODE_BINARY=node ../node_modules/react-native/scripts/react-native-xcode.sh

  1. (-1022) Your app ATS issue, check on Info.plist, and you can turn off ATS using below code: <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>
  1. (-1022) 您的应用 ATS 问题,请检查 Info.plist,您可以使用以下代码关闭 ATS: <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>

回答by Mehmet

For me I am using firebase framework on my project. I forgot the add GoogleService-Info.plistfile to my xcode project. After add it problem gone away.

对我来说,我在我的项目中使用了 firebase 框架。我忘了将GoogleService-Info.plist文件添加到我的 xcode 项目中。添加后问题就消失了。

回答by TheJizel

You probably need to sign the app.

您可能需要对应用程序进行签名。

Under the Project navigator, click on your app. Then select your target from the project and targets list. Within the "General" tab, find the 'Signing' section. You'll need to specify a team here.

在项目导航器下,单击您的应用程序。然后从项目和目标列表中选择您的目标。在“常规”选项卡中,找到“签名”部分。您需要在此处指定一个团队。

See this link for more info: https://developer.apple.com/library/content/documentation/IDEs/Conceptual/AppDistributionGuide/LaunchingYourApponDevices/LaunchingYourApponDevices.html#//apple_ref/doc/uid/TP40012582-CH27-SW4

有关更多信息,请参阅此链接:https: //developer.apple.com/library/content/documentation/IDEs/Conceptual/AppDistributionGuide/LaunchingYourApponDevices/LaunchingYourApponDevices.html#//apple_ref/doc/uid/TP40012582-CH27-SW4

回答by Sujit

I faced this issue where I was react-native-splash-screen. After removing it, it is working

我在 react-native-splash-screen 遇到了这个问题。删除后就可以使用了