This version of Astro is only compatible with Xcode 7.3
Menu

Developing with Astro

Astro is a hybrid SDK that leverages web technologies mixed with native code. This can present some challenges to app developers because you need to debug code in two places: Xcode for native code and Safari for javascript. This document points out some features of Astro to make this easier and presents some tips and tricks that the Astro team has found useful over time.

Astro Debugging Features

Debugging the worker and web content

Astro runs your application javascript in a hidden webview. WebViewPlugins also host web content that can contain javascript. You can debug any web content in your Astro app (including the worker's code, also known as app.js), by attaching the platform's browser to the webview you want to debug. On iOS use Safari, on Android use Google Chrome (note that these are the desktop browsers, not Mobile Safari or Mobile Chrome).

The Worker (aka app.js)

For all Astro apps there is a special webview that runs your application javascript. It is known as "the worker". The file that hosts the worker is always named index.html. To debug your application javascript, connect to this and you will see all of your application javascript (it will be a single, concatenated file and not separate files as they are when you are developing them). It is always named app.js. Many websites also name pages index.html, so you may need to take a few tries when attaching to get the right index.html. You will know you found the right one when the list of scripts includes app.js.

iOS

Safari Web Inspector Guide provides steps to attach desktop Safari to an iOS application's web views or worker.

Android

Remote Debugging on Android with Chrome provides steps to attach desktop Google Chrome to an Android application's web views or worker.

Astro.loggedErrors()

Errors in the javascript code of an Astro app can be hard to diagnose. Safari doesn't help things because it doesn't show any console messages that were posted prior to attaching to a WebView instance. To mitigate this, Astro provides a method that you can call in the Safari developer console to show errors that were posted up to that point. Simply call Astro.loggedErrors(). This will print all captured errors to the javascript console.

Tips and Tricks

Compiler Flags

At times it is useful to use compiler flags to conditionally run some code in one build configuration, but not any other. Astro includes two compiler flags: DEBUG and RELEASE to allow only running code in a development (DEBUG) or release (RELEASE) build.

To add a compiler flag to an Xcode project:

  1. Open the Project view.
  2. Select the main app target (typically named the same as your project) and select Build Settings.
  3. Find the section "Swift Compiler - Custom Flags".
  4. Expand the "Other Swift Flags" entry and select the "Debug" row. Once its selected wait a second and then click in the right column to begin editing the flag. Type in the new flag value prefixed with "-D" (eg. "-D DEBUG") and hit <Enter>.

Setting up a compiler flag

You can now write the following code to conditionally compile in DEBUG and RELEASE builds:

#if DEBUG
println("We're in a debug build.")
#elseif RELEASE
println("We're in a release build.")
#endif

Please Note: Use of conditional code should be kept to a minimum. It is really easy to write some code in one conditional but forget about the others and leave the code in a broken state.