Skip to main content

Overview

iOS apps running Gemma 4 via Google AI Edge SDK can use Grantex consent bundles for offline authorization. This guide covers Keychain storage, RS256 JWT verification with CryptoKit and Security framework, Ed25519 audit signing, and hash-chained audit logging in Swift.

Prerequisites

  • Xcode 15+ with iOS 16+ deployment target
  • Swift 5.9+
  • A Grantex account with API key and registered agent

Step 1: SwiftPM Setup

Add the following dependencies to your Package.swift or via Xcode’s package manager:
Alternatively, for a lighter approach, use Apple’s CryptoKit and Security framework directly (no third-party JWT library needed). This guide shows both approaches.

Step 2: Define the ConsentBundle Model

Step 3: Keychain Storage

Store the consent bundle in the iOS Keychain, which provides hardware-backed encryption on devices with Secure Enclave.

Step 4: JWT Verification with Security Framework

Verify RS256 JWTs using Apple’s Security framework. This avoids third-party dependencies and runs entirely on-device.

Step 5: Audit Logging with CryptoKit

Use CryptoKit for SHA-256 hashing and Ed25519 signing.

Step 6: Usage in a SwiftUI App

Security Considerations

  • Keychain protection class: kSecAttrAccessibleWhenUnlockedThisDeviceOnly prevents backup extraction and access when locked.
  • App Transport Security: ATS is enabled by default. The provisioning and sync calls to api.grantex.dev use HTTPS.
  • Jailbreak detection: Consider using frameworks like IOSSecuritySuite for high-security use cases. Jailbroken devices can extract Keychain items.
  • Background execution: If the agent runs in the background, ensure the Keychain item is accessible. Use kSecAttrAccessibleAfterFirstUnlock instead.
  • Biometric gate: Add kSecAccessControlBiometryCurrentSet to the Keychain item for Face ID / Touch ID protection before accessing the bundle.

Next Steps

Last modified on April 3, 2026