Debugging App When Launched by Push Notification

32,645

Solution 1

In Xcode < 4.0 (for Xcode >= 4, see answer by delirus below), you can now configure Xcode to attach the debugger to the app after you launch it, instead of launching the app through the debugger. This lets you debug things that vary based on the launch state of your application, such as URL schemes, pasteboards, and push notifications.

  1. In Xcode look in the Source bar, and below Targets there will be Executables.
  2. Bring up the inspector for your app in executables.
  3. Click on the Debugging tab tab in the inspector.
  4. Uncheck "Start executable after starting debugger"
  5. Check the "Wait for next launch/push notification"

Now when you click debug from Xcode instead of launching the app a window will display telling it is waiting for the app to launch. You then launch the app normally on the phone and Xcode attaches to it

Solution 2

For Xcode 4 you have to:

  1. Edit your active scheme via "Schemes" dropdown. (+<)
  2. Than choose your product - 'Run MyApp.app' on the left.
  3. Select 'Info' tab on the right.
  4. And finally choose "Wait for MyApp.app to launch" option.

More here in "Customize Executables in the Scheme Editor" section.

EDIT:
In case you miss logs in GDB, see Louis Gerbarg's comment to his answer.

Solution 3

For Xcode 10, 9, 8, 7 & 6:

  1. Select the Scheme on the toolbar (just left of the device you're debugging with)
  2. Choose Edit Scheme
  3. Select Run in the left panel
  4. For the Launch option, select Wait for executable to be launched
  5. Close the Scheme Editor and Run your Xcode project

Now you can debug when launching from a notification, custom url or by touching the app icon.

Solution 4

I'm not working with push notifications, so I don't know the exact working of push notifications. However, I'd try mimicking the receipt of a push notification by letting the main method of your program pretend a notification was posted if possible, or calling the application:didFinishLaunchingWithOptions: UIApplicationDelegate method from within the applicationDidFinishLaunching: method.

HTH - Cheers, Daan

Solution 5

In Xcode 6:

  1. connect iPhone to Macbook with USB
  2. launch the app on the iPhone by tapping the icon
  3. In Xcode menu, Debug > Attach to Process > Pick your App
Share:
32,645
Dutchie432
Author by

Dutchie432

There is a lot I could write here. Generally speaking, I am a (90% self-taught) computer programmer specializing in business related software utilizing .NET, PHP, JS, AJAX, jQuery, and XCode.

Updated on November 14, 2021

Comments

  • Dutchie432
    Dutchie432 over 2 years

    I am currently developing an app that receives Push Notifications. I have this all working 100% through a PHP page. There are several different types of push notifications my app can receive. The PHP handles this and sends different packets of information to my app which are all received just fine.

    However, when the users "views" the notification and my app launches, I obviously want to take a different action than I would if the user just launched the app manually - and on top of that, different actions depending on the push notification type. I have got this working fine... structurally.

    One of my push types is supposed to open a UIView that makes several connections to several different servers and negotiates data back and forth. This UIView works fine when, for example, triggered from the main menu - however when my push notification is triggering this UIView to appear, the socket connections are not acting as expected.

    Now my question is not about the sockets, but more so - How do you debug such a problem?? From what I can tell (I am relatively new) when the app launched from a push notification there is no way to link that execution to the debugger / console / etc... I am having a very difficult time trying to debug the code using UIAlertViews, as there are many lines of communication back and forth between the various servers.

    Any advice you have for me would be greatly appreciated.

  • Dutchie432
    Dutchie432 almost 15 years
    I know this may not be appropriate, but I love you. :)
  • Dutchie432
    Dutchie432 almost 15 years
    Also a good idea, but mimicking is always 2nd best to debugging the real thing! Happy Coding!
  • Dutchie432
    Dutchie432 almost 15 years
    Oh, one question though - my NSLog's dont seem to be printing...?
  • KeremV
    KeremV almost 15 years
    When you launch this way your application's stdin, stdout, stderr are not routed into GDB's console, they are pointed where they are in a normal app launch. You can see NSLog()s in the device's system console by looking at the device in the organizer.
  • Dutchie432
    Dutchie432 almost 15 years
    You taught me some very valuable points here. THANK YOU. I spent the past 2 days trying to debug something that I was able to fix within 20 minutes of your post.
  • DougW
    DougW over 13 years
    @Dutchie432 - No, but it can respond to registered URL schemes.
  • LoDani
    LoDani over 12 years
    I'm with Xcode 4.2 for Lion and, although the app is launching from the URL scheme correctly and the debugger execution is not stopping, the console is showing nothing. Am I missing something?
  • davis
    davis over 12 years
    @LoDani same here, did you ever figure out why?
  • Rodrigo
    Rodrigo over 12 years
    Lol.. I am not finding where are you saying, it is because you are using a xcode 3.x :) Can you update to XCode 4?
  • Ivan Vučica
    Ivan Vučica about 12 years
    @LoDani, Davis G.: Did you see Louis's comment on the accepted answer? stackoverflow.com/questions/1239000/… When launched this way, stdin/out/err do not get redirected into the console.
  • Eliot
    Eliot about 12 years
    Thank you, you saved my day! Trying to nail a bug that only occurs when my app is started by a url handler.
  • Dutchie432
    Dutchie432 about 12 years
    @delirus - Thanks for updating this for XCode 4.
  • matm
    matm about 12 years
    @Dutchie432: thanks and unupvote? ;) You're welcome anyway :)
  • Dutchie432
    Dutchie432 about 12 years
    I didn't unupvote.... I haven't been on this thread for a long time to even upvote it. I'm under the impression this was my first time even seeing this response.
  • matm
    matm about 12 years
    @Dutchie432: sorry for accusing, my mistake - the same time you commented someone did an unupvote. I was just interested in reasons behind downvoting. Cheers
  • dreyln
    dreyln over 11 years
    @stigi works in the simulator for me for testing URL schemes. I'm using xcode 4.5 if that matters
  • stigi
    stigi over 11 years
    @dreyln my comment was from 2009 :) a lot has changed since than. But thanks for the heads up!
  • Eric D'Souza
    Eric D'Souza over 9 years
    For those still having trouble finding the logs (like me), see owenfi's answer stackoverflow.com/a/25225961/1073754
  • Florin Odagiu
    Florin Odagiu almost 8 years
    This answer caused me a lot of trouble even though it generally set me on the right path. Step 3 is a little bit misleading. You have to do it, of course; select Run in the left panel. But, there should also be a step 5 with very similar conceptual wording: Click xcode's Run button, before expecting the results... Since in the beginning it did not work for me, I asked for advice from other coworkers...they asked me if I did the Run thing, to which I replied yes, thinking of option no. 3... There should really, really, really be a step 5 to this...
  • Zorayr
    Zorayr over 7 years
    You won't be be able to debug the critical launch path with this approach.
  • bgolson
    bgolson about 7 years
    @FlorinOdagiu Good call. Added step 5. Thanks!
  • Nate Whittaker
    Nate Whittaker about 6 years
    In case Xcode restores a different scheme tab, the Launch option is located under the Info tab.
  • David
    David over 5 years
    I'm not getting any output in the debugger window when I launch the app manually.