简体   繁体   English

首次启动时调试iOS应用

[英]Debugging iOS app on first launch

I have code that is triggered on launch only after the app is upgraded or installed. 我只有在升级或安装应用程序后才在启动时触发的代码。 I want to debug this code, but can't think of a way to do it. 我想调试此代码,但想不出一种方法。

I can't launch the app with Xcode because it would overwrite the existing app and wouldn't trigger the update. 我无法使用Xcode启动该应用程序,因为它会覆盖现有应用程序并且不会触发更新。

I can't attach Xcode to the process manually because the code I want to debug will have executed by the time I get Xcode attached. 我无法手动将Xcode附加到进程,因为要调试的代码将在附加Xcode时执行。

Is there someway of pulling this off that I'm missing? 有某种办法可以消除我所缺少的吗?

Use the "Wait for MyApp.app to launch" option. 使用“等待MyApp.app启动”选项。

In Xcode, hold down the Option key and choose Product->Run... (the ellipses are added when you hold Option down). 在Xcode中,按住Option键,然后选择“产品”->“运行...”(按住Option键时会添加省略号)。 This will bring up the scheme editor, with the Run scheme selected. 这将打开方案编辑器,并选择“运行方案”。 Click on the Info tab, and on the resulting Info panel you'll see a radio group labelled "Launch" with two options: "Automatically" and "Wait for MyApp.app to launch". 单击“信息”选项卡,然后在出现的“信息”面板上,您将看到一个标记为“启动”的广播组,带有两个选项:“自动”和“等待MyApp.app启动”。 If you select the second option, Xcode will start the debugger and wait for you to launch your application manually. 如果选择第二个选项,则Xcode将启动调试器,并等待您手动启动应用程序。 You can set a breakpoint at the beginning of the code you want to debug, and the debugger will stop there. 您可以在要调试的代码的开头设置一个断点,调试器将在此处停止。

Reset the app to its previous state. 将应用重置为以前的状态。

You may want to consider adding some code that resets the app to whatever state it was in before the upgrade. 您可能需要考虑添加一些代码,以将应用程序重置为升级前的状态。 This can be debug code that's excluded from the release build. 这可以是发布版本中排除的调试代码。 Debugging problems that require you to re-install the app every time you want to run through your debug can take a lot of time. 调试问题每次需要通过调试运行时都需要您重新安装应用程序,这会花费大量时间。 Even just having to restart the app at every debug cycle eats up a lot of your time. 即使只是在每个调试周期都必须重新启动应用程序,也会占用您很多时间。 It's very often worthwhile to spend a little extra time adding a button to your app that undoes whatever changes you're making so that you can do them again. 通常,花一些额外的时间在应用程序中添加一个按钮可以撤消所做的任何更改,以便您可以再次进行操作,这是值得的。

Unit test. 单元测试。

One great way to debug code that deals with app states that are difficult to recreate is to write unit tests. 调试处理难以重新创建的应用程序状态的代码的一种好方法是编写单元测试。 In a test, you can create any state you want. 在测试中,您可以创建所需的任何状态。 You can even create app states that may be impossible to recreate in your app. 您甚至可以创建应用程序状态,这些状态可能无法在您的应用程序中重新创建。 And you can debug that code over and over again. 您可以反复调试该代码。 Like the previous solution, it takes a little more time to write the code up front, but you'll save so much time on each iteration of your debug cycle that its well worth it. 像以前的解决方案一样,它需要花更多的时间来编写代码,但是您将在调试周期的每次迭代中节省大量时间,这是非常值得的。 Even better, you can add the test to your build process so to help ensure that the functionality doesn't break later. 更好的是,您可以将测试添加到构建过程中,以帮助确保功能以后不会破坏。

I also have some code that I only run on upgrades, i have this method below which I use at the end of applicationDidFinishLaunching. 我也有一些仅在升级时运行的代码,我有下面的方法,在applicationDidFinishLaunching末尾使用。 I can debug this too: 我也可以调试它:

- (void) determineWhetherToForceSync {
    NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];    
    NSString *savedVersionKey = @"savedVersionKey";
    NSString *savedVersion = [[NSUserDefaults standardUserDefaults] valueForKey:savedVersionKey];
    if (!savedVersion) {
        //This is a fresh install....

    } else {
        //They have a saved version, so check if it's the same as current version
        if (![currentVersion isEqualToString:savedVersion]) { //They have done an upgrade            
            //This is an update

        } //.... else they are starting the app on a second attempt
    }

    //Save the current version
    [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:savedVersionKey]; 
}

To re-test, delete the app and re-install. 要重新测试,请删除该应用并重新安装。 Or change your version number. 或更改您的版本号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM