Connecting Xcode To A Running Process

Did you know you can connect the Xcode debugger to a running process? You can also have Xcode wait for a process to launch before connecting. This helps when debugging issues when your App is launched in response to an external event such as a notification. We can also use it to peek at some of the built-in Apps in the simulator.

Connecting To A Running Process

The steps to connect the debugger to a running process:

  1. Build and run your App to install it on the simulator or device of your choosing. I find this works fine even with devices connected via WiFi.

  2. Stop the Xcode debug session. If you want to set some breakpoints for when the App is launched do that now. For example, to inspect the launch options you could set a breakpoint on didFinishLaunchingWithOptions in the App delegate.

  3. You can either launch the App and then attach the debugger or attach and then launch. Either way use the Xcode Debug menu to attach the debugger to a process:

    Xcode Debug menu

  4. If the App is not yet running you will need to attach to it by name. For example, here I am going to attach to the AdaptType project:

    Attach By Name

    If the process is not running the Xcode debugger will wait for it to start:

    Waiting for process

    This is useful if you want the App to launch in response to an external event such as a notification.

  5. If the App is already running you can connect to it directly either by name or finding it in the list of running processes. Make sure you have the device or simulator selected as the target of your Xcode project then use the “Attach to Process” option in the Debug menu:

    Likely Targets

    Xcode suggests the most likely process based on your current Xcode project and destination or you can find it in the list of running processes.

  6. Once attached you can debug as usual. The debugger will stop if it hits a breakpoint or you can use the view debugger to inspect the view hierarchy.

Peaking Into Some Apple Apps

You need to have permission to debug a process before you can connect to it. This prevents you from connecting to released Apps running on your device. However we can inspect some of Apple’s Apps running in the simulator.

For example, we can investigate the layout used by the Settings App.

  1. Run the Settings App on the iPhone 8 simulator.

  2. Make sure you have the iPhone 8 simulator set as the destination for an Xcode project. It doesn’t matter what the Xcode project is at this point.

  3. Use the Debug menu to attach the Debugger to the Preferences process. You can do this by name or search the list of running processes.

  4. Once the debugger attaches we can use the view debugger to stop the process and inspect its layout:

    View Debugger

    It’s no surprise that the Settings App is using table views. More interesting is that the slider for setting the text size at the bottom of the view is in a child view controller.

Try it with a few of the other pre-installed Apps and see what you can learn. Some process names to try: Contacts, Maps, Reminders, MobileSlideShow (photos) and SpringBoard.