Overview
The iOS setup for React Native Sherpa-ONNX uses CocoaPods and automatically downloads the sherpa-onnx XCFramework from GitHub Releases duringpod install. No manual framework downloads or Xcode configuration are required.
Requirements
- iOS 13.0+
- Xcode 14+
- CocoaPods or equivalent dependency manager
- Bundler (recommended for consistent Ruby/Pod versions)
Installation
1. Install the npm package
2. Install CocoaPods dependencies
What happens during pod install
The podspec automatically:
- Downloads the sherpa-onnx XCFramework (~80MB) from GitHub Releases
- Downloads libarchive sources for archive extraction support
- Configures build settings for device and simulator architectures
- Sets up header search paths for sherpa-onnx C++ API
third_party/sherpa-onnx-prebuilt/IOS_RELEASE_TAG and fetched from the GitHub Releases.
Architecture and Framework Structure
XCFramework Layout
The sherpa-onnx XCFramework includes static libraries for both device and simulator:Supported Architectures
- Device:
arm64(iPhone 5S and later, all iPads with A7+) - Simulator:
arm64(Apple Silicon Macs) andx86_64(Intel Macs)
Frameworks and Libraries
The Pod links the following iOS frameworks:- Foundation - Core system services
- Accelerate - Optimized math and DSP operations
- CoreML - Apple Neural Engine and on-device ML
- libc++ - C++ standard library
- libz - Compression (used by libarchive)
libarchive Integration
The iOS module compiles libarchive from source to support.tar.bz2 extraction (for model downloads). During pod install:
- Download:
ios/scripts/setup-ios-libarchive.shdownloads libarchive sources from GitHub Releases - Patch:
ios/scripts/patch-libarchive-includes.shadds required headers (stdio.h,unistd.h) to.cfiles - Compile: CocoaPods compiles the patched sources as part of the Pod build
third_party/libarchive_prebuilt/IOS_RELEASE_TAG.
libarchive is compiled from source because the prebuilt XCFramework approach doesn’t work reliably across all Xcode/SDK versions. Source compilation ensures compatibility.
Execution Provider: Core ML
iOS supports hardware acceleration via Core ML, which uses the Apple Neural Engine (ANE) when available.Checking Core ML Support
providerCompiled: Alwaystrue(Core ML is available on iOS 11+)hasAccelerator:trueif the device has an Apple Neural Engine (A12 Bionic and later, requires iOS 15+)canInit: Not currently implemented (returnsfalse)
Apple Neural Engine (ANE) Availability
The Apple Neural Engine is available on:- iPhone: XS/XS Max/XR and later (A12 Bionic+)
- iPad: iPad Air (3rd gen) and later, iPad mini (5th gen) and later, iPad Pro 2018 and later
- Mac: All Apple Silicon Macs (M1/M2/M3 series)
Build Configuration
The Pod configures build settings automatically viaSherpaOnnx.podspec:
Header Search Paths
Added automatically:ios/- iOS Objective-C++/Swift sourceios/archive/- Archive helper headersios/model_detect/- Model detection headersios/stt/,ios/tts/,ios/online_stt/- Feature-specific headers- XCFramework Headers (device + simulator slices)
- libarchive headers (downloaded sources)
Library Search Paths
Configured per SDK:- Device builds (
iphoneos*): Links toios-arm64/libsherpa-onnx.a - Simulator builds (
iphonesimulator*): Links toios-arm64_x86_64-simulator/libsherpa-onnx.a
Compiler Settings
- C++ Standard: C++17 (
CLANG_CXX_LANGUAGE_STANDARD) - C++ Library: libc++ (
CLANG_CXX_LIBRARY) - Force Load:
-lsherpa-onnx(ensures all symbols are included)
Preprocessor Definitions
For libarchive compatibility:Advanced: Building the Framework Yourself
If you need a custom sherpa-onnx build (e.g., different version or patches), you can build the XCFramework locally:Using the CI Workflow
The repository includes a GitHub Actions workflow that builds the XCFramework:.github/workflows/build-sherpa-onnx-ios-framework.yml
This workflow:
- Builds sherpa-onnx for iOS (device + simulator)
- Merges the C++ API static library (
libsherpa-onnx-cxx-api.a) - Creates an XCFramework with headers
- Publishes to GitHub Releases
Manual Build Steps
-
Build sherpa-onnx for iOS:
- Use upstream
build-ios.shfrom k2-fsa/sherpa-onnx - Or adapt the CI workflow’s build script
- Use upstream
-
Merge C++ API:
- The XCFramework must include the C++ API (
libsherpa-onnx-cxx-api.a) - The iOS Objective-C++ code uses
sherpa_onnx::cxx::*namespaces
- The XCFramework must include the C++ API (
-
Place the XCFramework:
-
Run pod install:
ios/Frameworks/sherpa_onnx.xcframework/ already exists and is valid.
Version Pinning
The iOS framework version is pinned in:framework-vX.Y.Z (e.g., framework-v1.12.24)
Troubleshooting
Framework download failed
Ifpod install fails with framework download errors:
-
Check network connectivity:
- The script downloads from GitHub Releases
- Corporate firewalls may block
api.github.com
-
Use a GitHub token (for rate limits):
-
Force re-download:
Framework headers not found
If Xcode can’t findsherpa-onnx/c-api/cxx-api.h:
-
Verify XCFramework structure:
Should contain
c-api.handcxx-api.h -
Clean and rebuild:
libarchive compilation failed
If you see errors about missing libarchive sources:-
Run the download script manually:
-
Check the downloaded sources:
Should contain
archive.h,archive_xxhash.h, and*.cfiles -
Check for patching errors:
Simulator vs. Device architecture mismatch
If you see architecture errors when switching between simulator and device:-
Clean build folder:
- In Xcode: Product → Clean Build Folder (⇧⌘K)
-
Verify XCFramework has both slices:
Should show:
ios-arm64/(device)ios-arm64_x86_64-simulator/(simulator)
Related Documentation
- Execution Providers - Detailed Core ML documentation
- Model Setup - Asset bundling for iOS
- Building sherpa-onnx - Upstream build instructions