如何在 ios 设备上未启动本机应用程序时进行调试

Posted

技术标签:

【中文标题】如何在 ios 设备上未启动本机应用程序时进行调试【英文标题】:how to debug when react native app doesnt start on ios device 【发布时间】:2016-08-08 09:10:13 【问题描述】:

当我启动 react-native 应用程序时,它总是会启动,只要我将它连接到 mac。

一旦我断开 iphone,该应用程序大多在启动时挂起并崩溃,没有任何进一步的错误消息。 但是,屏幕上会显示其他错误(红色和黄色标记的错误)。

当我双击主页按钮时,奇怪的是,应用程序似乎处于启动状态,但我无法切换到它。

如何最好地调试这种行为并找出导致奇怪启动行为的原因?

更新:

我发现了这些错误

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-cli: 1.0.0 反应原生:0.32.0-rc.0 反应:15.3.0

Xcode 8 beta 4、iOS 10

【问题讨论】:

在断开设备连接之前,您是否在 xcode 上遇到任何错误?您是否尝试在应用程序的根目录处渲染一个空组件并查看它是否有效?如果问题出在您的 JS 代码或本机部分,请尝试缩小范围。还有你使用的是什么 react-native 版本? 好的,我试试看。从 versino 0.28 开始问题仍然存在,现在我使用的是 0.32.rc-0 我在连接设备时更新了问题,但出现错误。 你解决了吗? 0.35 也有这个问题 【参考方案1】:

尝试在发布模式下构建应用。

编辑方案

选择运行选项卡 选择信息标签 将构建配置更改为发布 构建并运行项目

当您的设备连接到 Xcode 时,Xcode 会执行一些操作/优化并附加调试器。有时,此操作可防止显示错误。如果您在发布模式下运行您的应用程序,此操作将被忽略并且应用程序崩溃。

【讨论】:

非常感谢!我试图获取更多信息(请参阅更新的问题)您可能有线索吗?【参考方案2】:

您是否尝试过构建 jsBundle 以使用离线应用程序。 ?

【讨论】:

这应该自动完成吗?不是吗?我用 AppDelegate.m 更新帖子【参考方案3】:

我看到此错误是因为该应用正在调用 API 以从远程服务器获取数据,而该设备没有互联网连接。当设备连接到互联网时,错误消失了。

【讨论】:

【参考方案4】:

在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

【讨论】:

以上是关于如何在 ios 设备上未启动本机应用程序时进行调试的主要内容,如果未能解决你的问题,请参考以下文章

Facebook 请求配置文件 Graph API 无法在 iOS 7 本机 facebook 上未登录

iOS 设备上未显示开发人员菜单

在模拟器/设备上/使用或不使用 Chrome 调试时反应本机行为不同

PHP - APNs 消息已交付但在 iOS 设备上未收到

iOS 应用程序在设备上启动后连接调试器时冻结在空白屏幕上

EasyAPNS 发送消息但在 iOS 设备上未收到