Connecting Xcode To A Running ProcessOct 15, 2018 · 3 minute read
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:
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.
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
didFinishLaunchingWithOptionsin the App delegate.
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:
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
If the process is not running the Xcode debugger will wait for it to start:
This is useful if you want the App to launch in response to an external event such as a notification.
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:
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.
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.
Run the Settings App on the iPhone 8 simulator.
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.
Use the Debug menu to attach the Debugger to the
Preferencesprocess. You can do this by name or search the list of running processes.
Once the debugger attaches we can use the view debugger to stop the process and inspect its layout:
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.
Modern Auto Layout
If you enjoyed this post and want to learn more about using the view debugger you might also enjoy my new book on Auto Layout.