Core ML

Here's a quick setup guide for using custom Core ML models in iOS projects.


  1. A Core ML model file called Digits.mlmodel that recognizes the numbers 0-9 in an image.
  2. You have dragged this model into Xcode and can access the model let model = Digits() in your code.
  3. Your Fritz API key is api-key-12345
  4. Your Fritz AI model ID for the Digits model is model-id-abcde

1. Conform Your Model

In order to expose Fritz AI functionality to your Xcode generated Digits class, you must conform that class to a Fritz protocol that tells the SDK the about your API key and model id for that model.

Create a new file called :code:Digits+Fritz.swift and conform your class like so:

import Fritz
extension Digits: Fritz.SwiftIdentifiedModel {
static let modelIdentifier = "model-id-abcde"
static let packagedModelVersion = 1

2. Update Model Usage

By conforming the Digits class to the protocol we have exposed a :code:.fritz() function which injects an instrumented MLModel into the instance of that class.

Find any calls that look like this:
let model = Digits()

And change them to:

let model = Digits().fritz()

All of your prediction calls stay the same and they will be instrumented by the SDK.

3. Build and run your app

Test out each part of your app that uses the prediction, then look at the Fritz AI dashboard to see if data is showing up.


You do not have to bundle the mlmodel in your app package. You can download the model after a user installs your app. You may want to do this if you are trying to shrink the size of your initial app download.

Be aware of UX difficulties this may create. You will be unable to run any predictions until the download completes.

Here is an example of how to download the Digits model created above.

First you must generate and include the Digits.swift class file:

xcrun coremlc generate --language Swift Digits.mlmodel .

Then, add that generated file to your app: { url
guard url != nil else { return }
self.model = try! Digits(contentsOf: url).fritz()