当React Native应用在iOS设备上无法启动时,如何进行调试?

15
当我启动React Native应用时,只要连接到Mac,它就总是能够启动。但是一旦我断开iPhone连接,该应用程序大多数情况下会在启动时挂起并崩溃,没有任何进一步的错误消息。但是,其他错误将显示在屏幕上(标有红色和黄色)。当我双击Home按钮时,奇怪的是,应用程序似乎处于已启动状态,但我无法切换到它。如何最好地调试这种行为,并找到导致奇怪的启动行为的问题?更新:我发现了这些错误。
2016-08-08 16:25:23.604 [warn][tid:main][RCTEventEmitter.m:54] Sending `websocketFailed` with no listeners registered.
2016-08-08 16:25:23.612113 PhoenixApp[525:64778] Sending `websocketFailed` with no listeners registered.
2016-08-08 16:25:23.742 [warn][tid:com.facebook.react.JavaScript] The regenerator/runtime module is deprecated; please import regenerator-runtime/runtime instead.
2016-08-08 16:25:23.741927 PhoenixApp[525:65027] The regenerator/runtime module is deprecated; please import regenerator-runtime/runtime instead.
2016-08-08 16:25:23.860 [info][tid:com.facebook.react.JavaScript] Running application "PhoenixApp" with appParams: {"rootTag":1,"initialProps":{}}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF
2016-08-08 16:25:23.860251 PhoenixApp[525:65027] Running application "PhoenixApp" with appParams: {"rootTag":1,"initialProps":{}}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF
2016-08-08 16:25:24.139 [info][tid:com.facebook.react.JavaScript] null
2016-08-08 16:25:24.138808 PhoenixApp[525:65027] null

每秒之后

2016-08-08 16:31:43.159749 PhoenixApp[525:64956] [] __nw_connection_get_connected_socket_block_invoke 176 Connection has no connected handler
2016-08-08 16:31:45.372329 PhoenixApp[525:65445] [] __nw_connection_get_connected_socket_block_invoke 177 Connection has no connected handler
2016-08-08 16:31:47.575208 PhoenixApp[525:64955] [] __nw_connection_get_connected_socket_block_invoke 178 Connection has no connected handler
2016-08-08 16:31:49.788935 PhoenixApp[525:65445] [] __nw_connection_get_connected_socket_block_invoke 179 Connection has no connected handler
2016-08-08 16:31:51.970877 PhoenixApp[525:64955] [] __nw_connection_get_connected_socket_block_invoke 180 Connection has no connected handler
2016-08-08 16:31:54.173791 PhoenixApp[525:65445] [] __nw_connection_get_connected_socket_block_invoke 181 Connection has no connected handler

AppDelegate.m

/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

#import "AppDelegate.h"

#import "RCTBundleURLProvider.h"
#import "RCTRootView.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  NSURL *jsCodeLocation;

  [[RCTBundleURLProvider sharedSettings] setDefaults];
  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];

  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"PhoenixTrello"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  return YES;
}

@end

配置:

React Native 命令行工具: 1.0.0 React Native 版本: 0.32.0-rc.0 React 版本: 15.3.0

Xcode 8 beta 4, iOS 10


在断开设备之前,您是否在Xcode上遇到任何错误?您是否尝试在应用程序的根部渲染一个空组件并查看是否有效?尝试缩小问题范围,确定问题是在JS代码还是本地部分。另外,您使用的React-Native版本是多少? - nabn
好的,我会尝试。问题自0.28版本以来一直存在,现在我正在使用0.32.rc-0版本。 - radosch
我在设备连接的同时更新了带有错误的问题。 - radosch
你解决了吗?我们也遇到了0.35的这个问题。 - AlexDM0
4个回答

5

尝试以发布模式构建应用程序。

编辑方案

  • 选择“运行”选项卡
  • 选择“信息”选项卡
  • 更改“构建配置”为“发布”
  • 构建并运行项目

当您的设备连接到Xcode时,Xcode会执行一些操作/优化并附加调试器。有时这些操作会阻止显示错误。如果以发布模式运行应用程序,则会省略此类操作并且应用程序会崩溃。


非常感谢!我尝试获取更多信息(请参见更新的问题),也许你有线索? - radosch

0

我看到了这个错误,因为应用程序调用API从远程服务器获取数据,但设备没有网络连接。当设备连接到互联网时,错误消失了。


0

你尝试过构建jsBundle以便离线使用应用程序吗?


我更新了帖子并附上AppDelegate.m文件。 - radosch

0
在ios文件夹中,找到并替换Info.plist文件中的代码:
<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <false/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>localhost</key>
            <dict>
                <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                <true/>
            </dict>
        </dict>
    </dict>

使用

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

然后,运行:react-native run-ios

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接