How to print the hierarchy of views in iOS? [duplicate] - ios

Is there a GUI tool that inspects the view hierarchy of an iOS app? I'm thinking about Webkit's web inspector or similar tools. I'm looking to debug layout issues, like views having the wrong position or size, or a child not being properly contained in its parent. Currently I have to add asserts that test these various conditions by hand, or set different background colours on different views, and as you can imagine, that's a really tedious way to go about it.
I looked at Instruments's UI recorder, but that only records and plays back UI actions` and, in any case, works only for Mac apps.
Is there a better solution?

I don't know if there is a GUI view inspection tool, but I have had some luck with the debugging method on UIView: -recursiveDescription
if you pause the program in the debugger and input this into GDB (Edit: also works in LLDB)
po [[UIWindow keyWindow] recursiveDescription]
You get a printout of your entire view hierarchy. You can also call it on a specific view to get a printout of the view hierarchy of that view.
It can be a little tedious to wade through the info you get out of it, but it has proved useful to me.
Credit goes to this blog post which talked about this method and also linked to this helpful, but rather hard to find Apple tech note.

Xcode 6 now has 3D view hierarchy inspection built in like Reveal App and Spark Inspector.
Click on the "Debug View Hierarchy" button while your app is running to pause execution and inspect the views at the current moment.
More info at Apple's documentation.

Oddly enough, now there is another option, http://revealapp.com/, which as of this post is in an open (free) beta. As you can see it's another visual inspector.
EDIT 2014-04-05: Reveal is out of Beta and no longer free. There is a 30-day trial, however.

This question is old but let me put info here about new tool which I develop:
https://github.com/glock45/iOS-Hierarchy-Viewer

Just to keep this thread up to date, I've been recently playing with Spark Inspector. It's not free, but it's very nice.

The FLEX Debugger provides an in app view inspector that allows you to modify the UI in a running app. It also logs network requests.
https://github.com/Flipboard/FLEX

Free : Just type this in inspector :
po [[UIWindow keyWindow] recursiveDescription]
Commercial : http://revealapp.com/ I tested beta version of revealapp, it was good though has bugs.
Another Commercial tool : http://sparkinspector.com/ it's working seamless.

Swift 4.
iOS:
extension UIView {
// Prints results of internal Apple API method `recursiveDescription` to console.
public func dump() {
Swift.print(perform(Selector(("recursiveDescription"))))
}
}
macOS:
extension NSView {
// Prints results of internal Apple API method `_subtreeDescription` to console.
public func dump() {
Swift.print(perform(Selector(("_subtreeDescription"))))
}
}
Usage (in debugger): po myView.dump()

This dumps all in debug window.(Hard to read tho) :(
Working on iOS 10, Xcode 8.3.3
po UIApplication.shared.keyWindow?.recursiveDescription()

For swift/Xcode 10, enter this into the debug console:
po yourView.value(forKey: "recursiveDescription")!
It will print out a recursive hierarchy for any given UIView.
(Credit: How to debug your view hierarchy using recursiveDescription)

The approved answer no longer works for me, using Xcode 8 and Swift 2.3. Here's what does work for me:
po UIApplication.sharedApplication().keyWindow?.recursiveDescription()

Related

Black screen on app launch after latest Xcode 7.2 update

i am making a very complicated app with a lot of classes.
my app worked great and i worked on it for the last month.
the last time i worked on it i added a function and it worked great, i saved the app and ran it a few times since and it worked. the last time i turned my computer on, it prompted me that an update for xcode is available. i updated it, and since then every time i run my app it runs with no errors but on ios simulator it shows a black screen. what can i do? i worked really hard on that app. thanks for the help in advance.
There's few issues that could happened, withou detailed description of this issue, you can try following solutions:
reset simulator
check if your initial controller is set up in storyboard(select controller and press attribute inspector, select is initial view controller):
if you're setting initial view controller programmatically, check if that controller is not nil in app delegate
another tip - try to use UI debugger, that helps a lot:
Any debug messages in the Xcode output? That usually will give a clue.
One thing I can think of, is that in your
- (void) applicationDidFinishLaunching:(UIApplication*)application
delegate, try setting the window's root view controller to your view controller. Example:
[window setRootViewController:viewController];
[window makeKeyAndVisible];
Again, the Xcode debug output will confirm if this is indeed the case though.

How can you see the XCUIElement tree?

Background:
I'm experimenting with ui level testing in iOS 9.0 with XCode GM.
Question:
Is there a command in XCode GM that will allow you to see a 'tree' of accessible elements and their relationships? Something similar to the 'page' command in Appium?
Ideally I would be able to run a command in the debugger that would give me a list of elements available for selection/manipulation. Currently you can use debugDescription on a single XCUIElement but that only gives you info for that element.
Set a break point where you would like to see the tree... in the debugger type:
po print(XCUIApplication().debugDescription)
That prints out everything XCUITesting has access to. You can also just throw that in to your test:
func testTreeExample() {
XCUIApplication().buttons["login"].tap()
print(XCUIApplication().debugDescription)
XCUIApplication().buttons["next"].tap()
print(XCUIApplication().debugDescription)
}
Thta way if you are having trouble finding something you can have it automatically print out what the app sees right after you do something.
This isn't exactly what you're asking for, but Xcode’s Accessibility Inspector makes it much easier to look at your view hierarchy in terms of what elements are accessible via Identifiers. (N.B. It's not the "Label" in IB's Accessibility panel that matters, it's the "Identifier" field.):
In Xcode 7.2, open Xcode->Open Developer Tool->Accessibility Inspector. (You may need to give the app permission to run in System Preferences.) Then launch your iOS app from Xcode and hover over any UI element in the SIMULATOR. You’ll see comprehensive information about the element type, description, hierarchy, etc.
Anytime you record UI actions and the output doesn't look right, use the tool to figure out what accessibility descriptions need to be added, changed, or removed. (I spent a couple days trying to get a deeply embedded UISegmentedControl to change via the UI Test harness, and the problem became obvious once I figured out how to use the Accessibility Inspector tool.)
Thanks to the folks at shinobicontrols.com for the great tip!
I would suggest choosing from the menu bar: Debug > View Debugging > Capture View Hierarchy when running in debug. Not only do you a way of visually representing the views but also the left-side debug navigator shows the hierarchy. This may not be one-for-one with UI Testing's perspective but it can be very helpful. Hope that helps.
The way Appium does this is using Facebook WebdriverAgent.
As far as I can tell, the way they do it, essentially, is starting from the root application element and collecting information about each child, then recursing.
What about http://fbidb.io?
With idb ui describe-all command you get the accessibility information of all the elements on screen (not the entire app) https://fbidb.io/docs/commands#accessibility-info
Put a breakpoint in any of your tests then just do: po XCUIApplication() and that will print out the whole app's accessibility hierarchy in easy to read tree format.

Xcode incorrectly displaying the value of watch variables

I was scratching my head wondering why my rvc was set to nil when watching it while debugging through Xcode and thinking there must be something wrong with my project. So I created a project from scratch using the Xcode single view app template and then the only change I made was to add the following lines to didFinishLaunchingWithOptions:
Now look at what Xcode is displaying for the value of rvc in the watch window:
What's going on, why is it reporting rvc is nil?
Its not actually nil, but Xcode's watch window is reporting it as such. Is this a known issue with Xcode?
I found out the problem - its the positioning of the breakpoint, position it a line higher and rvc is not nil.
If you look at the watch window you can see rvc is light text when the other variables are bold text.
So my conclusion is at that point Xcode is reporting that rvc has gone out of scope and been deallocated, yet as the return statement has not yet executed at that point then rvc should not yet have gone out of scope.
Is it some Objective-C optimization thing going on resulting in this in Xcode or is it a defect with Xcode?
Either way its wasted a few hours of my time thinking I had a problem when there wasn't. In the future I need to make sure never to position breakpoints on a return statement again that contains local variables.

How do I inspect the view hierarchy in iOS?

Is there a GUI tool that inspects the view hierarchy of an iOS app? I'm thinking about Webkit's web inspector or similar tools. I'm looking to debug layout issues, like views having the wrong position or size, or a child not being properly contained in its parent. Currently I have to add asserts that test these various conditions by hand, or set different background colours on different views, and as you can imagine, that's a really tedious way to go about it.
I looked at Instruments's UI recorder, but that only records and plays back UI actions` and, in any case, works only for Mac apps.
Is there a better solution?
I don't know if there is a GUI view inspection tool, but I have had some luck with the debugging method on UIView: -recursiveDescription
if you pause the program in the debugger and input this into GDB (Edit: also works in LLDB)
po [[UIWindow keyWindow] recursiveDescription]
You get a printout of your entire view hierarchy. You can also call it on a specific view to get a printout of the view hierarchy of that view.
It can be a little tedious to wade through the info you get out of it, but it has proved useful to me.
Credit goes to this blog post which talked about this method and also linked to this helpful, but rather hard to find Apple tech note.
Xcode 6 now has 3D view hierarchy inspection built in like Reveal App and Spark Inspector.
Click on the "Debug View Hierarchy" button while your app is running to pause execution and inspect the views at the current moment.
More info at Apple's documentation.
Oddly enough, now there is another option, http://revealapp.com/, which as of this post is in an open (free) beta. As you can see it's another visual inspector.
EDIT 2014-04-05: Reveal is out of Beta and no longer free. There is a 30-day trial, however.
This question is old but let me put info here about new tool which I develop:
https://github.com/glock45/iOS-Hierarchy-Viewer
Just to keep this thread up to date, I've been recently playing with Spark Inspector. It's not free, but it's very nice.
The FLEX Debugger provides an in app view inspector that allows you to modify the UI in a running app. It also logs network requests.
https://github.com/Flipboard/FLEX
Free : Just type this in inspector :
po [[UIWindow keyWindow] recursiveDescription]
Commercial : http://revealapp.com/ I tested beta version of revealapp, it was good though has bugs.
Another Commercial tool : http://sparkinspector.com/ it's working seamless.
Swift 4.
iOS:
extension UIView {
// Prints results of internal Apple API method `recursiveDescription` to console.
public func dump() {
Swift.print(perform(Selector(("recursiveDescription"))))
}
}
macOS:
extension NSView {
// Prints results of internal Apple API method `_subtreeDescription` to console.
public func dump() {
Swift.print(perform(Selector(("_subtreeDescription"))))
}
}
Usage (in debugger): po myView.dump()
This dumps all in debug window.(Hard to read tho) :(
Working on iOS 10, Xcode 8.3.3
po UIApplication.shared.keyWindow?.recursiveDescription()
For swift/Xcode 10, enter this into the debug console:
po yourView.value(forKey: "recursiveDescription")!
It will print out a recursive hierarchy for any given UIView.
(Credit: How to debug your view hierarchy using recursiveDescription)
The approved answer no longer works for me, using Xcode 8 and Swift 2.3. Here's what does work for me:
po UIApplication.sharedApplication().keyWindow?.recursiveDescription()

Traversing the View Hierarchy on an iPhone or iPad [duplicate]

Is there a GUI tool that inspects the view hierarchy of an iOS app? I'm thinking about Webkit's web inspector or similar tools. I'm looking to debug layout issues, like views having the wrong position or size, or a child not being properly contained in its parent. Currently I have to add asserts that test these various conditions by hand, or set different background colours on different views, and as you can imagine, that's a really tedious way to go about it.
I looked at Instruments's UI recorder, but that only records and plays back UI actions` and, in any case, works only for Mac apps.
Is there a better solution?
I don't know if there is a GUI view inspection tool, but I have had some luck with the debugging method on UIView: -recursiveDescription
if you pause the program in the debugger and input this into GDB (Edit: also works in LLDB)
po [[UIWindow keyWindow] recursiveDescription]
You get a printout of your entire view hierarchy. You can also call it on a specific view to get a printout of the view hierarchy of that view.
It can be a little tedious to wade through the info you get out of it, but it has proved useful to me.
Credit goes to this blog post which talked about this method and also linked to this helpful, but rather hard to find Apple tech note.
Xcode 6 now has 3D view hierarchy inspection built in like Reveal App and Spark Inspector.
Click on the "Debug View Hierarchy" button while your app is running to pause execution and inspect the views at the current moment.
More info at Apple's documentation.
Oddly enough, now there is another option, http://revealapp.com/, which as of this post is in an open (free) beta. As you can see it's another visual inspector.
EDIT 2014-04-05: Reveal is out of Beta and no longer free. There is a 30-day trial, however.
This question is old but let me put info here about new tool which I develop:
https://github.com/glock45/iOS-Hierarchy-Viewer
Just to keep this thread up to date, I've been recently playing with Spark Inspector. It's not free, but it's very nice.
The FLEX Debugger provides an in app view inspector that allows you to modify the UI in a running app. It also logs network requests.
https://github.com/Flipboard/FLEX
Free : Just type this in inspector :
po [[UIWindow keyWindow] recursiveDescription]
Commercial : http://revealapp.com/ I tested beta version of revealapp, it was good though has bugs.
Another Commercial tool : http://sparkinspector.com/ it's working seamless.
Swift 4.
iOS:
extension UIView {
// Prints results of internal Apple API method `recursiveDescription` to console.
public func dump() {
Swift.print(perform(Selector(("recursiveDescription"))))
}
}
macOS:
extension NSView {
// Prints results of internal Apple API method `_subtreeDescription` to console.
public func dump() {
Swift.print(perform(Selector(("_subtreeDescription"))))
}
}
Usage (in debugger): po myView.dump()
This dumps all in debug window.(Hard to read tho) :(
Working on iOS 10, Xcode 8.3.3
po UIApplication.shared.keyWindow?.recursiveDescription()
For swift/Xcode 10, enter this into the debug console:
po yourView.value(forKey: "recursiveDescription")!
It will print out a recursive hierarchy for any given UIView.
(Credit: How to debug your view hierarchy using recursiveDescription)
The approved answer no longer works for me, using Xcode 8 and Swift 2.3. Here's what does work for me:
po UIApplication.sharedApplication().keyWindow?.recursiveDescription()

Resources