Skip to content

fxnai/fxnios

Repository files navigation

Function for iOS

function logo

Dynamic JSON Badge

Run Python functions (a.k.a "predictors") locally in your iOS apps, with full GPU acceleration and zero dependencies.

Tip

Join our waitlist to bring your custom Python functions and run them on-device across Android, iOS, macOS, Linux, web, and Windows.

Note

Function requires iOS 15+.

Installing Function

Function is distributed as a SwiftPM package, and can be added as a dependency in an Xcode project or in a SwiftPM package:

In Xcode

In your project editor, open the Package Dependencies tab, search for https://github.com/fxnai/fxnios.git and add the package to your project:

Xcode2

In Swift Package Manager

Add the following dependency to your Package.swift file:

let package = Package(
    name: "MyAwesomeApp",
    dependencies: [
        .package(url: "https://github.com/fxnai/fxnios.git", from: "0.0.1"),
    ],
    targets: [
        .target(name: "MyAwesomeApp", dependencies: ["FunctionSwift"]),
    ]
)

Retrieving your Access Key

Before creating predictions, you will need to create a Function account. Once you do, generate an access key:

generate access key

Next, create an fxn.xcconfig build configuration file and enter your access key:

# Function access key
FXN_ACCESS_KEY="<ACCESS KEY>"

Caution

Make sure that your fxn.xcconfig file is excluded from source control by adding it to your .gitignore file. Never share your access key.

Making a Prediction

First, create a Function client:

import FunctionSwift

// 💥 Create a Function client
let fxn = Function(accessKey: "...")

Then make a prediction:

// 🔥 Make a prediction
let prediction = try await fxn.predictions.create(
    tag: "@fxn/greeting",
    inputs: ["name": "Sam"]
)

Finally, use the prediction results:

// 🚀 Use the results
print("Prediction result: \(prediction.results![0]!)")

Embedding Predictors

Function normally works by downloading and executing prediction functions at runtime. But because iOS requires strict sandboxing, you must download and embed predictors at build-time instead. First, create an fxn.config.swift file at the root of your target directory:

import FunctionSwift

let config = Function.Configuration(
    // add all predictor tags to be embedded here
    tags: [
        "@fxn/greeting"
    ]
)

Next, right click on your project and run the Embed Predictors command on your app target:

Embed

Function will download the prediction function as a dynamic framework, then configure Xcode to embed the framework into your app bundle.

Note

The Embed Predictors script requires internet and file system access to download and embed the prediction function into your Xcode project.

Important

After embedding, Xcode might prompt you to either reload the project from disk or keep the current version in memory. Always reload your project from disk.

Useful Links

Function is a product of NatML Inc.