Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust Swift FFI

A Swift Package that exposes Rust logic via UniFFI, distributed as an XCFramework.


Prerequisites

  • Rust
  • Xcode Command Line Tools (xcode-select --install)
  • A valid Apple Developer certificate installed in your Keychain (required only for signing)

Building

1. Clone the repository

git clone https://github.com/OpenSwiftXO/RustSwiftFFI.git
cd RustSwiftFFI

2. Run the build script

./build_xcframework.sh

This will:

  1. Install the required Rust targets (aarch64-apple-ios, aarch64-apple-ios-sim, aarch64-apple-darwin)
  2. Compile the Rust library for each target in release mode
  3. Generate Swift bindings, C headers, and a modulemap via UniFFI
  4. Bundle everything into build/rust_swiftFFI.xcframework
  5. Copy the Swift wrapper to build/RustSwift.swift

3. Output files

File Description
build/rust_swiftFFI.xcframework XCFramework containing static libraries for iOS, iOS Simulator, and macOS
build/RustSwift.swift Generated Swift wrapper to include in your app target

Code Signing

By default, signing is skipped. To sign the XCFramework (required for App Store distribution):

Step 1 — Find your signing identity:

security find-identity -v -p codesigning

You will see output like:

1) XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX "Apple Development: Nguyen Van A (TEAMID)"

Step 2 — Run the build with your identity:

SIGNING_IDENTITY="Apple Development: Nguyen Van A (TEAMID)" ./build_xcframework.sh

Or export it for the current shell session:

export SIGNING_IDENTITY="Apple Development: Nguyen Van A (TEAMID)"
./build_xcframework.sh

Step 3 — Verify the signature:

codesign -dvvv build/rust_swiftFFI.xcframework

Distributing & Integrating via Swift Package Manager (Remote Binary Target)

SPM recommends distributing XCFrameworks as a remote binary target — a zipped .xcframework hosted at a public URL with a SHA-256 checksum for verification. Do not commit the .xcframework directly into the repository.

Step 1 — Zip the XCFramework

cd build
zip -r rust_swiftFFI.xcframework.zip rust_swiftFFI.xcframework

Step 2 — Compute the SHA-256 checksum

swift package compute-checksum build/rust_swiftFFI.xcframework.zip

Copy the printed hash (e.g. a1b2c3d4e5f6...).

Step 3 — Upload the zip

Upload rust_swiftFFI.xcframework.zip to a publicly accessible URL, such as a GitHub Release asset:

  1. Create a new GitHub Release for your repository
  2. Attach rust_swiftFFI.xcframework.zip as a release asset
  3. Copy the download URL (e.g. https://github.com/OpenSwiftXO/RustSwiftFFI/releases/download/1.0.0/rust_swiftFFI.xcframework.zip)

Step 4 — Declare the binary target in Package.swift

.binaryTarget(
    name: "rust_swiftFFI",
    url: "https://github.com/OpenSwiftXO/RustSwiftFFI/releases/download/1.0.0/rust_swiftFFI.xcframework.zip",
    checksum: "a1b2c3d4e5f6..."  // paste the hash from Step 2
)

Step 5 — Add the Swift wrapper

Commit build/RustSwift.swift into your package repository and add it to your library's source target in Package.swift.


Simply call the exposed functions directly from your own Swift code:

let result = addCoreLogic(left: 1, right: 2)
let greeting = sayHiFromRust()

Example Implementation

See the RustSwiftFFI-SPM repository for a complete example of how to integrate this XCFramework into a Swift Package Manager project.


Documentation & References

UniFFI-rs – Official Mozilla UniFFI documentation and source code

About

RustSwiftFFI: A Demo of Rust-Swift Integration via UniFFI

Topics

Resources

Stars

Watchers

Forks

Releases

Contributors

Languages