Three bugs I shipped in a Capacitor iOS app, and how small the fixes were

# javascript# capacitor# ios# react
Three bugs I shipped in a Capacitor iOS app, and how small the fixes wereJoão from UMDIA

I build UMDIA, an iPhone app for learning European Portuguese. It's React and Vite wrapped in...

I build UMDIA, an iPhone app for learning European Portuguese. It's React and Vite wrapped in Capacitor, with Firebase behind it. The web-to-native setup has been good to me, but it has a few traps that don't show up in the browser. These three made it into App Store builds before I caught them.

1. A Capacitor plugin returned from a promise calls .then on the native side

App Check never worked in version 1.0. No crash and no visible error: the token request just never happened. The cause was this pattern:

async function loadAppCheck() {
  const { FirebaseAppCheck } = await import('@capacitor-firebase/app-check')
  await FirebaseAppCheck.initialize(options)
  return FirebaseAppCheck // the bug
}
Enter fullscreen mode Exit fullscreen mode

A Capacitor plugin is a Proxy. Any property you read on it becomes a call to a native method. When an async function returns a value, JavaScript checks whether that value is a thenable by reading .then. On the proxy, that read turns into a native call, and iOS answers with FirebaseAppCheck.then() is not implemented on ios. The promise rejects, and if nothing logs it, the feature silently does nothing.

The fix is to never let the plugin be the value of a promise. Wrap it:

return { plugin: FirebaseAppCheck }
Enter fullscreen mode Exit fullscreen mode

The same thing applies to any plugin you lazy-load, so I now wrap the text-to-speech plugin the same way. If you see ".then() is not implemented" in a Capacitor log, this is almost certainly it.

2. sort(() => Math.random() - 0.5) is not a shuffle

The quiz and flashcards kept showing the same words, and the correct answer landed in the first slots more often than it should. I had shuffled with the classic one-liner:

const shuffled = words.sort(() => Math.random() - 0.5)
Enter fullscreen mode Exit fullscreen mode

A comparator that returns random results breaks the assumptions of the sort algorithm. The output depends on the engine's sort implementation, and it's biased: items tend to stay near where they started. With a dictionary in alphabetical order, the early words won far more often than the late ones.

Fisher-Yates gives every ordering the same probability, and it's six lines:

export function shuffle(arr, rnd = Math.random) {
  const r = [...arr]
  for (let i = r.length - 1; i > 0; i--) {
    const j = Math.floor(rnd() * (i + 1));
    [r[i], r[j]] = [r[j], r[i]]
  }
  return r
}
Enter fullscreen mode Exit fullscreen mode

Passing rnd in also makes it testable: give it a seeded generator and check the distribution over many runs.

3. The iPhone voice spelled out "NIF"

The app pronounces words with the system's speech synthesis, using the European Portuguese voice. That means no audio files, it works offline and it covers the whole dictionary. One problem: Portuguese reads some acronyms as words. Your tax number, the NIF, is said "nif", one syllable. The voice said "N, I, F".

Lower case didn't help. The voice still spelled "nif", and I could hear it, but I also measured it: "o nif" took longer to say than "o ene i efe". So the next update swaps a few acronyms for a spelling the voice reads as a word before speaking:

const ACRONYMS_AS_WORDS = { NIF: 'nife', IBAN: 'iban', CEP: 'cep' }
Enter fullscreen mode Exit fullscreen mode

"nife" rhymes with "bife", and the final "e" in European Portuguese is almost silent, so it comes out as "nif". It's a hack, but a deliberate one with a test next to it.

The common thread

None of these showed up in the desktop browser during development. Two of them only exist on a real device, and the third only shows up as a pattern over hundreds of rounds. What helped most was writing a small test for each one once I understood it, so they stay fixed.

If you're learning European Portuguese, or you're curious what a Capacitor app looks like in the App Store, UMDIA is free to start: App Store · umdia.co