For app development on iOS and MacOS, Xcode is our world. Xcode has evolved significantly over the years. Although Xcode 9 is amazingly complex, it becomes evident during use that Xcode 9 is very effective at developing world-class apps for both the iPhone and iPad families of devices, Mac, and other Apple devices. As such, there are numerous Xcode tips and short-cuts that one learns along the path on the app development journey. Here is my growing list:
Uncabling the attached iPhone/iPad from Xcode
Until Xcode 9, actual iPhone/iPad devices needed to be cabled via USB to the Mac for app development with Xcode. With Xcode 9, wireless access to iOS devices is available. Not only is this a time saver when switching between devices for testing a new build, but it reduces the number of cables attached to the suite of available ports on the Mac.
To enable this feature:
- Launch Xcode 9 and connect the soon-to-be wireless iOS device via a USB to the Mac.
- Verify that the cabled iOS device is powered on with the screen not locked (the home screen is visible).
- On Xcode 9, open ‘Window -> Devices and Simulators‘.
The annotated dialog box in the following screenshot shows the remaining steps left to do on Xcode 9:
- Click on the listed device to become wireless.
- Check the box titled ‘Connect via network‘.
At this point, Xcode 9 will establish a wireless connection to the selected device. When the little internet symbol shows up next the connected device as shown in the dialog screenshot, then the device (e.g., iPad) can be unplugged. Xcode 9 development now supports wireless development access to the selected device. Ths will be evident when selecting a device for build and run.
Accessing app ‘Documents’ folder and files on the iPhone/iPad
On iOS devices, an app’s folders and files are grouped into containers that resides in the app’s sandbox directory. This container and folder/file arrangement is depicted in the following diagram (ref: Apple File System Programming Guide). During installation of a new app, these containers are created. The data container consists of a set of top-level folders that the app can use to organize user app data with subfolders and files.
Let’s assume the app you’re developing with Xcode writes a file to the ‘Documents‘ folder on an iPhone or iPad. How do you manually verify that a file was created within the ‘Documents‘ folder on an actual iOS device and that the contents are correct?
The ‘Documents‘ folder and files aren’t directly accessible from the Mac after a test run of an app during an Xcode session. The trick is to download the data container as part of an app package from the iOS device to some selected folder on the Mac.
Here are the steps using a recent example development app running on my iPad:
- Verify that the iOS device is powered on with the home screen not locked.
- From within Xcode 9, open ‘Window -> Devices and Simulators‘ which brings up the following screenshot (annotated to show next steps).
- Click on the listed device in the left pane to select it.
- From the list of apps, click on the app whose ‘Documents‘ folder is being retrieved.
- Click on the little settings icon as shown in the figure and select ‘Download Container …‘. Select a folder to place the app package container.
Open ‘Finder‘ and locate the folder containing the downloaded package (wrapper that includes the data container) that is identified by its rather long filename with a suffix ‘.xcappdata‘. An example screenshot of this package is shown.
Use a ‘2-finger tap‘ (or right click) on the filename and select ‘Show Package Contents‘ as shown in the following screenshot.
The ‘Finder‘ window now shows the containers with their folders and files similar to the following screenshot. Follow the path to the ‘Documents‘ folder where, in my case, sits a ‘sqlite‘ database file that was created by my simple example app. At this point, I copy this file out of the package to a separate folder to open and evaluate its contents.
In this specific case since I want to evaluate the contents of a ‘sqlite‘ database file, I use a downloaded Mac app, DB Browser for SQLite.
Commenting out lines of code
Sometimes it is helpful to comment out a section of code as part of app development and debugging. Instead of typing ‘//‘ on every line of code to disable it in Xcode’s editor window, use ‘⌘/‘ (i.e., command + slash) to apply commenting prefixes ‘//‘ to several rows at the same time. It is a toggle keyboard shortcut, so highlighting commented rows and using ‘⌘/‘ will remove those commenting prefixes.
Opening separate editor windows for source files
Double-clicking on a filename in the projects view (left pane on Xcode) opens the file in a separate edittable resizeable floating window. Doing this with multiple source files provides a nice way to see contents of several source files at the same time with the ability to arrange and size each of the editor windows independently. Personally, I like this approach since I can keep the Xcode app on my MBP screen and the source file editors on the second large monitor. I can also increase the size of the Debug area within the Xcode app.
Moving lines of code around in a file
Sometimes I want to move code (a single line, a group of lines, or even an entire function) around within a file. There are two really nice keyboard shortcuts for doing this operation. Place the cursor on the line or highlight a group of lines of code. Press ⌥⌘[ (i.e., option + command + leftbracket) to move the selection up a line or ⌥⌘] (i.e., option + command + rightbracket) to move the selection down a line. And, if I want to increase or decrease the indentation for that highlighted code segment, the keyboard shortcuts are similar. Press ⌘[ (i.e., command + leftbracket) to outdent or ⌘] (i.e., command + rightbracket) to indent. Very handy!
I will continue to add to this collection of Xcode tips as I discover them!