React Native Swift & Promises

A Quick Reference Guide

Mike Surowiec
Dev Shack
2 min readMar 1, 2017

--

For any given React Native project, I prefer to use Swift on the native side and promises on the Javascript side. Here’s the skinny on how to make that happen.

1. Bridging Header File

You need a .h file to expose Objective-C libraries to your Swift file. This file is called `ProjectName-Bridging-Header.h` and usually includes a couple of the react native libraries that you want to access.

ProjectName-Bridging-Header.h

2. React Native <-> Obj-C <-> Swift

You’ll need another file (.m) that describes your Swift class in an Objective-C way so that React Native can understand it. The functions written here map to functions exposed from the Swift class that you’ll see in the next section. As an example, I’ll use parts of an AudioManager class that I’ve written for the Autism Music Project App.

AudioManager.m

3. Your Swift Class

Woo! Finally we get to write some Swift. This is where the bulk of the logic for the AudioManager class lives.

AudioManager.swift

4. Using it in Javascript

Now that we’re back in Javascript (a.k.a wonderland 😉) we can use all our JS tricks like async & await when talking over the native bridge.

AudioComponent.js

I hope you found that quick run-through helpful, and if you have any questions or need clarification please ask!

Also make sure to check out the Autism Music Project, which is a collaboration to create specially designed music for children with Autism Spectrum Disorders.

Thanks!

mikesurowiec

--

--