You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
12 KiB

  1. # FLEX
  2. [![CocoaPods](https://img.shields.io/cocoapods/v/FLEX.svg)](http://cocoapods.org/?q=FLEX)
  3. [![CocoaPods](https://img.shields.io/cocoapods/l/FLEX.svg)](https://github.com/Flipboard/FLEX/blob/master/LICENSE)
  4. [![CocoaPods](https://img.shields.io/cocoapods/p/FLEX.svg)]()
  5. [![Twitter: @ryanolsonk](https://img.shields.io/badge/contact-@ryanolsonk-blue.svg?style=flat)](https://twitter.com/ryanolsonk)
  6. [![Build Status](https://travis-ci.org/Flipboard/FLEX.svg?branch=master)](https://travis-ci.org/Flipboard/FLEX)
  7. [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
  8. FLEX (Flipboard Explorer) is a set of in-app debugging and exploration tools for iOS development. When presented, FLEX shows a toolbar that lives in a window above your application. From this toolbar, you can view and modify nearly every piece of state in your running application.
  9. ![View Hierarchy Exploration](http://engineering.flipboard.com/assets/flex/basic-view-exploration.gif)
  10. ## Give Yourself Debugging Superpowers
  11. - Inspect and modify views in the hierarchy.
  12. - See the properties and ivars on any object.
  13. - Dynamically modify many properties and ivars.
  14. - Dynamically call instance and class methods.
  15. - Observe detailed network request history with timing, headers, and full responses.
  16. - Add your own simulator keyboard shortcuts.
  17. - View system log messages (e.g. from `NSLog`).
  18. - Access any live object via a scan of the heap.
  19. - View the file system within your app's sandbox.
  20. - Browse SQLite/Realm databases in the file system.
  21. - Trigger 3D touch in the simulator using the control, shift, and command keys.
  22. - Explore all classes in your app and linked systems frameworks (public and private).
  23. - Quickly access useful objects such as `[UIApplication sharedApplication]`, the app delegate, the root view controller on the key window, and more.
  24. - Dynamically view and modify `NSUserDefaults` values.
  25. Unlike many other debugging tools, FLEX runs entirely inside your app, so you don't need to be connected to LLDB/Xcode or a different remote debugging server. It works well in the simulator and on physical devices.
  26. ## Usage
  27. In the iOS simulator, you can use keyboard shortcuts to activate FLEX. `f` will toggle the FLEX toolbar. Hit the `?` key for a full list of shortcuts. You can also show FLEX programatically:
  28. Short version:
  29. ```objc
  30. // Objective-C
  31. [[FLEXManager sharedManager] showExplorer];
  32. ```
  33. ```swift
  34. // Swift
  35. FLEXManager.shared().showExplorer()
  36. ```
  37. More complete version:
  38. ```objc
  39. #if DEBUG
  40. #import "FLEXManager.h"
  41. #endif
  42. ...
  43. - (void)handleSixFingerQuadrupleTap:(UITapGestureRecognizer *)tapRecognizer
  44. {
  45. #if DEBUG
  46. if (tapRecognizer.state == UIGestureRecognizerStateRecognized) {
  47. // This could also live in a handler for a keyboard shortcut, debug menu item, etc.
  48. [[FLEXManager sharedManager] showExplorer];
  49. }
  50. #endif
  51. }
  52. ```
  53. ## Feature Examples
  54. ### Modify Views
  55. Once a view is selected, you can tap on the info bar below the toolbar to present more details about the view. From there, you can modify properties and call methods.
  56. ![View Modification](http://engineering.flipboard.com/assets/flex/advanced-view-editing.gif)
  57. ### Network History
  58. When enabled, network debugging allows you to view all requests made using NSURLConnection or NSURLSession. Settings allow you to adjust what kind of response bodies get cached and the maximum size limit of the response cache. You can choose to have network debugging enabled automatically on app launch. This setting is persisted across launches.
  59. ![Network History](http://engineering.flipboard.com/assets/flex/network-history.gif)
  60. ### All Objects on the Heap
  61. FLEX queries malloc for all the live allocated memory blocks and searches for ones that look like objects. You can see everything from here.
  62. ![Heap Exploration](http://engineering.flipboard.com/assets/flex/heap-browser.gif)
  63. ### Simulator Keyboard Shortcuts
  64. Default keyboard shortcuts allow you to activate the FLEX tools, scroll with the arrow keys, and close modals using the escape key. You can also add custom keyboard shortcuts via `-[FLEXMananger registerSimulatorShortcutWithKey:modifiers:action:description]`
  65. ![Simulator Shortcuts](https://cloud.githubusercontent.com/assets/1422245/10002927/1106fd32-6067-11e5-8e21-57a357c259b6.png)
  66. ### File Browser
  67. View the file system within your app's sandbox. FLEX shows file sizes, image previews, and pretty prints `.json` and `.plist` files. You can copy text and image files to the pasteboard if you want to inspect them outside of your app.
  68. ![File Browser](http://engineering.flipboard.com/assets/flex/file-browser.gif)
  69. ### SQLite Browser
  70. SQLite database files (with either `.db` or `.sqlite` extensions), or [Realm](http://realm.io) database files can be explored using FLEX. The database browser lets you view all tables, and individual tables can be sorted by tapping column headers.
  71. ![Database Browser](https://cloud.githubusercontent.com/assets/1422245/11786700/d0ab95dc-a23c-11e5-80ce-0e1b4dba2b6b.png)
  72. ### 3D Touch in the Simulator
  73. Using a combination of the command, control, and shift keys, you can simulate different levels of 3D touch pressure in the simulator. Each key contributes 1/3 of maximum possible force. Note that you need to move the touch slightly to get pressure updates.
  74. ![Simulator 3D Touch](https://cloud.githubusercontent.com/assets/1422245/11786615/5d4ef96c-a23c-11e5-975e-67275341e439.gif)
  75. ### System Library Exploration
  76. Go digging for all things public and private. To learn more about a class, you can create an instance of it and explore its default state.
  77. ![System Libraries Browser](http://engineering.flipboard.com/assets/flex/system-libraries-browser.gif)
  78. ### NSUserDefaults Editing
  79. FLEX allows you to edit defaults that are any combination of strings, numbers, arrays, and dictionaries. The input is parsed as `JSON`. If other kinds of objects are set for a defaults key (i.e. `NSDate`), you can view them but not edit them.
  80. ![NSUserDefaults Editor](http://engineering.flipboard.com/assets/flex/nsuserdefaults-editor.gif)
  81. ### Learning from Other Apps
  82. The code injection is left as an exercise for the reader. :innocent:
  83. ![Springboard Lock Screen](http://engineering.flipboard.com/assets/flex/flex-readme-reverse-1.png) ![Springboard Home Screen](http://engineering.flipboard.com/assets/flex/flex-readme-reverse-2.png)
  84. ## Installation
  85. FLEX requires an app that targets iOS 7 or higher.
  86. ### CocoaPods
  87. FLEX is available on [CocoaPods](https://cocoapods.org/pods/FLEX). Simply add the following line to your podfile:
  88. ```ruby
  89. pod 'FLEX', '~> 2.0', :configurations => ['Debug']
  90. ```
  91. ### Carthage
  92. Add the following to your Cartfile:
  93. ```
  94. github "flipboard/FLEX" ~> 2.0
  95. ```
  96. ### Manual
  97. Manually add the files in `Classes/` to your Xcode project.
  98. ## Excluding FLEX from Release (App Store) Builds
  99. FLEX makes it easy to explore the internals of your app, so it is not something you should expose to your users. Fortunately, it is easy to exclude FLEX files from Release builds. The strategies differ depending on how you integrated FLEX in your project, and are described below.
  100. At the places in your code where you integrate FLEX, do a `#if DEBUG` check to ensure the tool is only accessible in your `Debug` builds and to avoid errors in your `Release` builds. For more help with integrating FLEX, see the example project.
  101. ### FLEX added with CocoaPods
  102. CocoaPods automatically excludes FLEX from release builds if you only specify the Debug configuration for FLEX in your Podfile.
  103. ### FLEX added with Carthage
  104. If you are using Carthage, only including the `FLEX.framework` in debug builds is easy:
  105. 1. Do NOT add `FLEX.framework` to the embedded binaries of your target, as it would otherwise be included in all builds (therefore also in release ones).
  106. 1. Instead, add `$(PROJECT_DIR)/Carthage/Build/iOS` to your target _Framework Search Paths_ (this setting might already be present if you already included other frameworks with Carthage). This makes it possible to import the FLEX framework from your source files. It does not harm if this setting is added for all configurations, but it should at least be added for the debug one.
  107. 1. Add a _Run Script Phase_ to your target (inserting it after the existing `Link Binary with Libraries` phase, for example), and which will embed `FLEX.framework` in debug builds only:
  108. ```shell
  109. if [ "$CONFIGURATION" == "Debug" ]; then
  110. /usr/local/bin/carthage copy-frameworks
  111. fi
  112. ```
  113. Finally, add `$(SRCROOT)/Carthage/Build/iOS/FLEX.framework` as input file of this script phase.
  114. <p align="center"><img src="README-images/flex-exclusion-carthage.jpg"/></p>
  115. ### FLEX files added manually to a project
  116. In Xcode, navigate to the "Build Settings" tab of your project. Click the plus and select `Add User-Defined Setting`.
  117. ![Add User-Defined Setting](http://engineering.flipboard.com/assets/flex/flex-readme-exclude-1.png)
  118. Name the setting `EXCLUDED_SOURCE_FILE_NAMES`. For your `Release` configuration, set the value to `FLEX*`. This will exclude all files with the prefix FLEX from compilation. Leave the value blank for your `Debug` configuration.
  119. ![EXCLUDED_SOURCE_FILE_NAMES](http://engineering.flipboard.com/assets/flex/flex-readme-exclude-2.png)
  120. ## Additional Notes
  121. - When setting fields of type `id` or values in `NSUserDefaults`, FLEX attempts to parse the input string as `JSON`. This allows you to use a combination of strings, numbers, arrays, and dictionaries. If you want to set a string value, it must be wrapped in quotes. For ivars or properties that are explicitly typed as `NSStrings`, quotes are not required.
  122. - You may want to disable the exception breakpoint while using FLEX. Certain functions that FLEX uses throw exceptions when they get input they can't handle (i.e. `NSGetSizeAndAlignment()`). FLEX catches these to avoid crashing, but your breakpoint will get hit if it is active.
  123. ## Thanks & Credits
  124. FLEX builds on ideas and inspiration from open source tools that came before it. The following resources have been particularly helpful:
  125. - [DCIntrospect](https://github.com/domesticcatsoftware/DCIntrospect): view hierarchy debugging for the iOS simulator.
  126. - [PonyDebugger](https://github.com/square/PonyDebugger): network, core data, and view hierarchy debugging using the Chrome Developer Tools interface.
  127. - [Mike Ash](https://www.mikeash.com/pyblog/): well written, informative blog posts on all things obj-c and more. The links below were very useful for this project:
  128. - [MAObjCRuntime](https://github.com/mikeash/MAObjCRuntime)
  129. - [Let's Build Key Value Coding](https://www.mikeash.com/pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html)
  130. - [ARM64 and You](https://www.mikeash.com/pyblog/friday-qa-2013-09-27-arm64-and-you.html)
  131. - [RHObjectiveBeagle](https://github.com/heardrwt/RHObjectiveBeagle): a tool for scanning the heap for live objects. It should be noted that the source code of RHObjectiveBeagle was not consulted due to licensing concerns.
  132. - [heap_find.cpp](https://www.opensource.apple.com/source/lldb/lldb-179.1/examples/darwin/heap_find/heap/heap_find.cpp): an example of enumerating malloc blocks for finding objects on the heap.
  133. - [Gist](https://gist.github.com/samdmarshall/17f4e66b5e2e579fd396) from [@samdmarshall](https://github.com/samdmarshall): another example of enumerating malloc blocks.
  134. - [Non-pointer isa](http://www.sealiesoftware.com/blog/archive/2013/09/24/objc_explain_Non-pointer_isa.html): an explanation of changes to the isa field on iOS for ARM64 and mention of the useful `objc_debug_isa_class_mask` variable.
  135. - [GZIP](https://github.com/nicklockwood/GZIP): A library for compressing/decompressing data on iOS using libz.
  136. - [FMDB](https://github.com/ccgus/fmdb): This is an Objective-C wrapper around SQLite
  137. ## Contributing
  138. Please see our [Contributing Guide](https://github.com/Flipboard/FLEX/blob/master/CONTRIBUTING.md).
  139. ## TODO
  140. - Swift runtime introspection (swift classes, swift objects on the heap, etc.)
  141. - Improved file type detection and display in the file browser
  142. - Add new NSUserDefault key/value pairs on the fly