beetlebug

Develop for iOS

This page is the loop of a developer of an iOS app: try the script on the Mac, run it in the Simulator, see its output, and make the app for a device.

Try the script on the Mac

The same script runs in a window of the Mac:

zig-out/bin/ztk main.tcl

tk touchscreen gives 0 on the Mac and 1 on a phone, so a script can choose a layout. The debug tools of Develop on macOS and the tests with --headless work on the Mac.

Run a script in the Simulator

ztk --ios makes the app, starts a simulator when none runs, installs the app, starts it, and prints the output of the app until the app ends:

zig-out/bin/ztk --ios main.tcl images/
ztk: org.ztk.run.main runs in the simulator C788CA2A-...; its output follows. Ctrl-C stops the output.
ztk: renderer metal
ztk: org.ztk.run.main ended
Option or variable What it does
--device NAME Selects the simulator: a name or a UDID of xcrun simctl list devices. With no --device: ZTK_IOS_SIMULATOR, else a booted iPhone, else the first iPhone of the newest iOS runtime.
ZTK_IOS_NO_OPEN=1 Keeps the Simulator app closed.
The words after the script Files and folders of the app, as for ztk bundle.
--allow, --tcl Add capabilities to file, channels, and clock, and select the Tcl release.

The app has the id org.ztk.run.NAME and stays in the simulator. xcrun simctl ui booted appearance dark turns on the dark mode of the Simulator, and the app follows it.

See the output

puts and the errors of the script go to the output of the app. ztk --ios, simctl launch --console-pty, and devicectl device process launch --console show it in the terminal.

Each line also goes to the unified log of the system, so an app that you start from its icon has its output there too: in the Console app of the Mac, with the device or the simulator selected and the name of the app in the search field, or in a terminal:

# The Simulator:
xcrun simctl spawn booted log stream --predicate 'process == "Hello"'
``` To give the app an environment variable, add the prefix `SIMCTL_CHILD_` for `simctl` or `DEVICECTL_CHILD_` for `devicectl`, as in `DEVICECTL_CHILD_ZTK_STATS=1`.

## Make the app

For the Simulator:

```sh
zig-out/bin/ztk bundle -o Hello.app --target aarch64-ios-simulator \
    --id org.example.hello main.tcl images/
xcrun simctl install booted Hello.app
xcrun simctl launch --console-pty booted org.example.hello

For an iPhone or an iPad, give the identity and the profile of a developer:

# The names of the identities: security find-identity -v -p codesigning
export ZTK_IOS_SIGN_IDENTITY="Apple Development: ..."
export ZTK_IOS_PROFILE=$HOME/Downloads/Hello.mobileprovision
zig-out/bin/ztk bundle -o Hello.app --target aarch64-ios \
    --id org.example.hello main.tcl images/
Option What it does
-o Name.app Writes an app folder.
-o Name.ipa Writes a zip of Payload/Name.app for the target aarch64-ios. It needs the identity and the profile.
--target T aarch64-ios-simulator or x86_64-ios-simulator for the Simulator, and aarch64-ios for a device.
--id ID The bundle identifier. It must match the app ID of the profile, or the profile must have an app ID of TEAM.*.
--name NAME The name under the icon.
--version V The version. The default is 1.0.
--icon FILE.png The icon of the home screen, a square PNG. iOS needs 1024 pixels with no transparency, so ztk bundle scales a smaller icon up and puts the icon on white. iOS rounds the corners, so the icon fills the whole square, with no margin. actool of Xcode compiles it into Assets.car.
--ios-plist FILE Keys to add to Info.plist, from a plist in XML (Info.plist and permissions).
--ios-privacy FILE The privacy manifest of the app (The privacy manifest).
--ios-identity ID The signing identity. The default is ZTK_IOS_SIGN_IDENTITY.
--ios-profile FILE The provisioning profile. The default is ZTK_IOS_PROFILE.
--allow CAPS The capabilities. The default is file,channels,clock.

With an identity and a profile, ztk bundle puts the profile in the app, gives the app the entitlements of the profile, and signs it with codesign. With no identity, the app gets an ad hoc signature: the Simulator accepts it, and a device refuses it.

Install the app on a device

xcrun devicectl list devices
xcrun devicectl device install app --device DEVICE Hello.app
xcrun devicectl device process launch --console \
    --device DEVICE org.example.hello

The device needs Developer Mode on (Settings, Privacy and Security), and it must trust the Mac. When devicectl lists the device as unavailable:

  1. Connect the device with a cable.
  2. Unlock the device, and accept "Trust This Computer".
  3. Open the Devices and Simulators window of Xcode one time.

The next page, Widgets, shows the widgets on an iPhone and an iPad.