How to find a code for a specific view displayed in a simulator or on the device?


I’m debugging a complex application with lots of protocols, view controllers etc… The app is a variation of VIPER architecture. The app is written in Swift 3 and uses iOS 10.3. The codebase is huge and it is about two years old of active development and architectural tweaks, however I’m new to the app.

One of the bugs is that one of the workflows in the app shows a wrong screen at the end of it. I am able to replicate the bug every time in the simulator.

My question is how to find the code (ex. ViewController or ViewPresenter) of the view currently displayed in the simulator? Can I pause the program with lldb and somehow get to the code?


@tomn Thanks very much for your question! When it comes to finding the identity of the view, the easiest approach that I’ve used (and I currently am working on a project that also utilizes a similar variation of VIPER) is:

  1. Identify text contained in the view. This could be labels, Navigation Bar title, text of any kind. Once you have identified this text, search your project for occurrences of this text in Xcode to see where it is found, and through a process of elimination, determine what class controls the view. Once you’ve narrowed it down, place a break point at the “viewDidLoad” of the class you think controls the view. This way, you’ll know when the view is about to appear, the break point will be hit.

  2. Another approach is to identify the screen that comes BEFORE it. It appears you have identified the screen prior to it, so go through the code, and step through the code in that class, and determine which method is triggering the call to transition to the next View. Once you’ve identified the code that triggers the call, see where it leads to, and that should point to the code that is calling the screen in question.

I hope this helps!

All the best!

An easy way to peruse UIViewControllers as they appear. If you are running on a physical device, replace $rdi with $x0. If you are running on 32-bit hardware, I can’t take you seriously :slight_smile:

It works like magic. This is what I was looking for. I’m looking forward for the book update. Thanks for help, now it makes my work a ton easier.

@lolgrep Thanks very much for your contribution! Much appreciated :slight_smile:

1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.