Running Titanium Apps from Xcode: A Workaround

If you've ever tried running a Titanium app directly from Xcode, you might have encountered a problem: the app gets stuck at the splash screen. This can be a frustrating experience, especially if you're used to the ease of running apps from the Titanium CLI.

Why would you want to run a Titanium app from Xcode in the first place? There are several reasons:

  1. Module Development: If you're developing a native module for Titanium, you might want to debug your code from Xcode to take advantage of its debugging tools.
  2. Debugging the SDK: If you're debugging the Titanium SDK itself, running the app from Xcode allow for placing breakpoints in native Titanium methods.
  3. Other Native Debugging:  Xcode will allow you to inspect code at a deeper level than the JavaScript debugger tools available in Visual Studio Code

However, when you build a Titanium app in Xcode, it doesn't automatically include the necessary JavaScript files and image assets that the Titanium CLI includes in its build. These missing files can cause the app to hang at the splash screen, unable to proceed further.

To solve this issue, we can leverage Xcode's ability to run custom scripts during the build process. We'll create a script that copies the necessary files from the Titanium CLI's build directory to Xcode's build directory whenever you build your project.

Here's the script:

# Define source directory

# Get the build settings
BUILD_SETTINGS=$(xcodebuild -project ${PROJECT_NAME}.xcodeproj -scheme ${TARGET_NAME} -showBuildSettings)

# Extract the value of the TARGET_BUILD_DIR setting
DST_DIR=$(echo "${BUILD_SETTINGS}" | grep TARGET_BUILD_DIR | cut -d'=' -f2 | xargs)

# Remove last component
DST_DIR=$(dirname "$DST_DIR")

# Add the app name to the path

# Copy files from source to destination, skipping existing files
rsync -av --ignore-existing "${SRC_DIR}/" "${DST_DIR}/"

This script uses the rsync command to copy files from one directory to another, skipping any files that already exist in the destination directory.

To add this script to your Xcode project, follow these steps:

  1. Open your project in Xcode.
  2. Select the project file from the file list to view the project settings.
  3. Make sure your target is selected in the next column.
  4. Select the "Build Phases" tab.
  5. Click on the plus button (+) at the top of the tab.
  6. Select "New Run Script Phase".
  7. In the new run script phase, enter the script provided above.

Now, every time you build your project in Xcode, this script will run. It will copy the necessary files from the Titanium CLI's build directory to Xcode's build directory, ensuring that the app has everything it needs to run successfully.

Once you've finished your debugging session in Xcode and you're ready to return to the Titanium CLI, it's a good idea to run ti clean from the command line. This command cleans up the build directories, ensuring that any changes made during the Xcode debugging session don't interfere with subsequent builds from the Titanium CLI.

This solution is a workaround and might not solve all issues related to running a Titanium app directly from Xcode. The recommended way to run a Titanium app is still via the Titanium CLI, which handles all these details for you. Running a Titanium app directly from Xcode can be complex and might require additional setup and configuration.

However, for those situations where you need to dive deeper into the native layer of your Titanium app, this script can be a lifesaver. It bridges the gap between the Titanium CLI's thorough preparation of the app and Xcode's more direct approach to building and running the app. With it, you can get the best of both worlds and run your Titanium app directly from Xcode, with all the necessary files in place.

Remember: Always keep track of any changes you make to your project's build settings or scripts, and be ready to revert them if you encounter problems. And don't forget to run ti clean after you finish debugging with Xcode to ensure a clean slate for your next Titanium CLI build. Happy coding!