ios-swiftui/README.md
The sherpa-onnx Swift package provides two products for iOS:
sherpa-onnx — uses a static library (.a) inside an xcframework. The sherpa-onnx code is compiled directly into your app binary at build time. This is the default and recommended option for most apps.sherpa-onnx-shared — uses a shared library (.dylib / .framework) inside an xcframework. The sherpa-onnx code is packaged as a separate dynamic framework and loaded at runtime. Your app bundle will contain an additional .framework file.Both products expose the same Swift API. The only difference is how the native C++ code is linked.
| Product | Library Type | Swift Import |
|---|---|---|
sherpa-onnx | Static library (.a in xcframework) | import SherpaOnnx |
sherpa-onnx-shared | Shared library (.dylib in xcframework) | import SherpaOnnxShared |
There are two ways to add sherpa-onnx to your Xcode project:
https://github.com/k2-fsa/sherpa-onnx
1.13.0 ..< 2.0.01.13.4mastersherpa-onnx or sherpa-onnx-shared) and click Add PackageIf you have the sherpa-onnx source code checked out locally:
sherpa-onnx or sherpa-onnx-shared) and click Add PackageAlternatively, you can manually edit the .pbxproj file:
/* XCLocalSwiftPackageReference section */
C96194D3301EED750025FCCA /* XCLocalSwiftPackageReference "../../path/to/sherpa-onnx" */ = {
isa = XCLocalSwiftPackageReference;
relativePath = "../../path/to/sherpa-onnx";
};
After adding the package, you should see it listed in:
The sherpa-onnx package provides two library variants, each backed by its own xcframework:
| Product | XCFramework Type | Swift Import |
|---|---|---|
sherpa-onnx | Static xcframework (default) | import SherpaOnnx |
sherpa-onnx-shared | Shared/dynamic xcframework | import SherpaOnnxShared |
sherpa-onnx links statically — the sherpa-onnx code is compiled directly into your app binary. This is the default and recommended option for most apps.sherpa-onnx-shared links dynamically — the sherpa-onnx code lives in a separate .framework that is embedded into your app bundle at runtime.sherpa-onnx or sherpa-onnx-shared)You do not need to manually change your import statements when switching between sherpa-onnx and sherpa-onnx-shared. Use #if canImport(...) in your Swift code to automatically detect which product is available:
#if canImport(SherpaOnnx)
import SherpaOnnx
#elseif canImport(SherpaOnnxShared)
import SherpaOnnxShared
#else
#error("SherpaOnnx module not found. Please check your SPM dependency configuration.")
#endif
This way the same source code works with either product. The #if canImport(...) check runs at compile time — if SherpaOnnx is available it uses that, otherwise it falls back to SherpaOnnxShared. If neither is found, the build fails with a clear error message.
File → Packages → Reset Package Caches
# Clear SPM artifact cache
rm -rf ~/Library/Caches/org.swift.swiftpm
# Clear Xcode DerivedData
rm -rf ~/Library/Developer/Xcode/DerivedData
In Xcode: Product → Clean Build Folder (Shift+Cmd+K)
If you see an error like:
checksum of downloaded artifact of binary target 'SherpaOnnxIOS' (...) does not match checksum specified by the manifest (...)
This means Xcode's SPM cache has a stale artifact. Fix:
File → Packages → Reset Package CachesIf you see Missing package product 'sherpa-onnx-shared':
File → Packages → Resolve Package VersionsIf you see No such module 'SherpaOnnxShared' or No such module 'SherpaOnnx':
sherpa-onnx or sherpa-onnx-shared) to your target#if canImport(...) as recommended above, the code will automatically pick the right moduleIf you see an error like:
duplicate output file '...SherpaOnnxC.framework' on task: Copy ...
This is a known Xcode issue with SPM dynamic (shared) frameworks — Xcode sometimes generates duplicate "Copy" build phases. Fix:
rm -rf ~/Library/Developer/Xcode/DerivedData/SherpaOnnxTts-*
File → Packages → Reset Package CachesIf it still happens, open the target's Build Phases tab in Xcode and look for duplicate Embed Frameworks or Copy Files phases that both reference SherpaOnnxC.framework. Remove the duplicate if found.