# DBDebugToolkit [![CI Status](http://img.shields.io/travis/dbukowski/DBDebugToolkit.svg?style=flat)](https://travis-ci.org/dbukowski/DBDebugToolkit) [![Version](https://img.shields.io/cocoapods/v/DBDebugToolkit.svg?style=flat)](http://cocoapods.org/pods/DBDebugToolkit) [![License](https://img.shields.io/cocoapods/l/DBDebugToolkit.svg?style=flat)](http://cocoapods.org/pods/DBDebugToolkit) [![Platform](https://img.shields.io/cocoapods/p/DBDebugToolkit.svg?style=flat)](http://cocoapods.org/pods/DBDebugToolkit) [![Twitter: @darekbukowski](https://img.shields.io/badge/contact-@darekbukowski-blue.svg?style=flat)](https://twitter.com/darekbukowski) ## Features - [Performance](#performance) - [User interface](#user-interface) - [Network](#network) - [Resources](#resources) - [Console](#console) - [Simulating location](#simulating-location) - [Crash reports](#crash-reports) - [Custom variables](#custom-variables) - [Custom actions](#custom-actions) - [Shortcut items](#shortcut-items) ### Performance The first submenu is dedicated to measuring your application performance. You can find there statistics divided into three sections: * CPU Includes current CPU usage, max CPU usage recorded and a chart displaying the CPU usage over time.
* Memory Includes current memory usage, max memory usage recorded and a chart displaying the memory usage over time. Additionally it has a "Simulate memory warning" option.
* FPS Includes current FPS, min FPS recorded and a chart displaying the FPS value over time.
#### Widget On top of the performance submenu you can see a switch that enables the widget displaying current CPU usage, memory usage and frames per second. The widget will stay on top of the screen and will refresh every one second. It can be moved around the screen with pan gesture. Tapping on the widget opens the performance submenu with tapped section (CPU, memory or FPS).
#### Simulating memory warning In the memory section you can also find the "Simulate memory warning" button. Use it to check if you handle the warning properly. ### User interface DBDebugToolkit provides many useful tools for UI debugging: * Showing view frames
* Slow animations
* Showing touches (for screen sharing and recording)
If your device supports 3D Touch, the touch indicator view's alpha value will depend on the touch force. * Grid overlay
Grid overlay is highly customizable. You can choose the size of the grid from values between 4 and 64. There are also 10 available color settings and a slider for adjusting the opacity. Due to the fact that the device screen dimensions are not always a multiple of the selected size of the grid, the grid lines are drawn from left, right, top and bottom, so you can check if your layout was implemented properly. The remaining middle parts of the grid can have a different size, which is indicated by two labels. The grid overlay stays on top of the screen at all times. * Autolayout trace
* Current view description
* View controller hierarchy
* Available font families with preview
* Showing `UIDebuggingInformationOverlay` `UIDebuggingInformationOverlay` is a powerful tool discovered by [Ryan Peterson](https://twitter.com/ryanipete) in the private API. Once you set up DBDebugToolkit, you can show `UIDebuggingInformationOverlay` either by selecting it in the user interface section or by tapping the status bar with two fingers. Make sure to check [Ryan's article]( http://ryanipete.com/blog/ios/swift/objective-c/uidebugginginformationoverlay/) to know more about the features it provides.
Tap on any request on the list to see its details. From the request details screen you can also access the request and response body preview, that handles image, text and JSON data.
#### User defaults and keychain Browsing the user defaults and the keychain is very similar. For both of those data sources you can see a list of all the stored key-value pairs. Every pair can be removed by swiping the cell left and tapping the delete button. On the right of the navigation bar there is also a clear button that allows you to remove all the values stored in the currently browsed data source.
It's difficult to see all the managed object's attributes in a table view cell, so you can tap one of them to present a new view with all the details about the selected managed object. In the first section you can see all the attributes and their values. The second section has all the relationships. You can tap any of them to see the related objects. In case of to-one relationships you will be redirected to the same view, but this time it will display the details of the related object:
In case of to-many relationship you will see a view with a list of the related objects:
On top of the view presenting the managed objects list you can see a filter button that allows you to filter the objects by their attributes and sort the results.
#### Cookies Browsing cookies is similar to browsing user defaults and keychain, but this time the stored data are not key-value pairs anymore. First screen displays the list of all the cookies, but only the most important data are shown: names and domains of the cookies. You can select a cookie cell to see its details on a new view. You can delete one cookie by swiping left on its cell and tapping delete button or by opening the details screen and using the navigation bar button. You can also clear all the cookies by using the clear button on the right of the navigation bar in the cookies list view.
### Console DBDebugToolkit displays console output inside a UITextView. The text view will be automatically scrolled down to display the new output, unless you are currently reading older entries above.
The captured console output can be easily shared by email. The share button will open a mail compose view controller with prefilled subject containing build information and body containing device and system information and the console output.
**Warning!** Capturing both stderr and stdout is very complicated. Stderr and stdout could be both redirected to a file that would be displayed in the text view. However, that data should be redirected back to stderr and stdout, so that you could for example see it in the console. Unfortunately it would be impossible to distinguish which data came from stderr and which came from stdout, so everything would be redirected to one of them. DBDebugToolkit is meant to be noninvasive, so this solution was unacceptable. Instead, stdout and stderr are observed in the background. The only drawback is that if you would have a loop sending data alternately to stdout and stderr you could notice a slight change in the received data order. If for some reason it is not acceptable in your project you can disable console output capturing: ```swift import DBDebugToolkit func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { DBDebugToolkit.setup() DBDebugToolkit.setCapturingConsoleOutputEnabled(false) return true } ``` ### Simulating location Simulating location with DBDebugToolkit is really straightforward. You can either select a location from the predefined list (the same as on iOS simulator) or choose any point on a map. This choice is remembered even after you relaunch the application, which should be really helpful when you work on a feature that depends on the user location.
### Crash reports DBDebugToolkit prepares and collects reports about your application crashes. Every crash report is saved in a file and can be seen in the menu. It contains information about the reason and the time of the crash occurence, app & system version, a screenshot captured at the moment of the crash, the whole console output and a stack trace. It can be easily shared by email, so that you can quickly report a bug to a person that can fix it.
Please remember to remove the registered actions to avoid retaining the view controller by the variable objects: ```swift import DBDebugToolkit override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) let titleVariable = DBDebugToolkit.customVariable(withName: "View title") titleVariable?.removeTarget(self, action: nil) // The same for the remaining variables. } ``` If those variables are only related to that one view controller, you can also remove them completely, so that the list of variables presented in the menu is always related to the current state of your application: ```swift import DBDebugToolkit override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) DBDebugToolkit.removeCustomVariables(withNames: ["View title", "Shows cell numbers", "Number of columns", "Minimum interitem spacing"]) } ``` ### Custom actions If you need an easy access to any action performed by your application, you can use the custom actions feature of DBDebugToolkit. After the setup you can add as many custom actions as you like: ```swift import DBDebugToolkit func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { DBDebugToolkit.setup() let sendReportAction = DBCustomAction(name: "Send report") { // Your code responsible for sending the report. } let clearDatabaseAction = DBCustomAction(name: "Clear database") { // Your code responsible for clearing the database. } DBDebugToolkit.add([sendReportAction, clearDatabaseAction]) return true } ``` To run these actions simply open the menu and go to custom actions section. You will see the list of all the actions. Tap on any of them to perform it.
### Shortcut items What would you do if one of the testers in your team had a crash on the app launch caused by invalid data stored in the keychain? To avoid wiping all content and settings from the device, you would probably have to prepare a special build that removes the keychain data. This can be avoided with DBDebugToolkit, as it comes now with a shortcut item that removes all the data from the keychain, user defaults, documents directory and cookies.
To enable that option, all you have to do is add one line of code after DBDebugToolkit setup: ```swift DBDebugToolkit.addClearDataShortcutItem() ``` Please note that overriding `UIApplication.shared.shortcutItems` after that line of code will remove this shortcut item. **Warning!** Shortcut items are available on iOS 9.0 and above. Also, you can only use them on devices that support 3D Touch.