Caius Theory

Now with even more cowbell…

Stop HealthKit causing SIGABRT

You have some crazy idea for an iOS app that uses HealthKit so you fire up Xcode, create a new project & add the HealthKit entitlement. Follow the tutorial to request authorization from the HKHealthKitStore. Hit run to make sure the app compiles and find that it instantly crashes with a SIGABRT in AppDelegate.

Puzzled by this you go over the minimal amount of code you've added and pare it right down to just the HKHealthKitStore.requestAuthorization call which is still causing the SIGABRT as soon as the app tries to boot.

The missing piece of the puzzle is Info.plist needs a key adding to it for the HealthKit authorisation screen. The documentation helpfully forgets to mention this however. Here's some quick simple steps to fix it:

  1. Open Info.plist in Xcode
  2. Click the (+) at the top to add a new key/value to the file
  3. Enter "Privacy - Health Share Usage Description" for the key
  4. Enter a useful message to the user explaining why they should allow access to their healthkit data for your app for the value
  5. Run your app and see the HealthKit authorisation sheet appear

NB: if you want to update/write any data to healthkit, you'll need to add the "Privacy - Health Update Usage Description" key with a description as well.

Compile & run swift files directly

Turns out you can run a swift file without having to compile it into a binary somewhere and then run that binary. Makes swift behave a bit more like a scripting language like ruby or python when you need it to.

Using the xcrun binary, we can reach into the current Xcode /bin folder and run binaries within there. So xcrun swift lets you run the swift binary to compile files for instance. If you view the help with -h, there's a useful flag -i listed there:

-i    Immediate mode

Turns out immediate mode means "compile & run" in the same command, which is what we're after.

$ cat hello.swift
println("Hello World")

$ xcrun swift -i hello.swift
Hello World

Bingo. But what if we want to make hello.swift executable and call it directly without having to know it needs the swift binary to call it. Unix lets files define their shebang to say how the file needs to be executed, so lets go for that here too!

$ cat hello2.swift
#!/usr/bin/env xcrun swift -i
println("Hello World 2")

$ chmod +x hello2.swift
$ ./hello2.swift
Hello World 2

No more having to fire up Xcode for quick CLI tools, especially ones using the system frameworks!