<!--
{
  "documentType" : "article",
  "framework" : "CoreML",
  "identifier" : "/documentation/CoreML/encrypting-a-model-in-your-app",
  "metadataVersion" : "0.1.0",
  "role" : "article",
  "title" : "Encrypting a Model in Your App"
}
-->

# Encrypting a Model in Your App

Encrypt your app’s built-in model at compile time by adding a compiler flag.

## Discussion

Tell Xcode to encrypt your model as it compiles your app by adding a compiler flag to your build target.

### Add a compiler flag

In Xcode, navigate to your project’s target and open its Build Phases tab. Expand the Compile Sources section and select the model you want Xcode to encrypt at compile time. Open the model’s compiler flags editor by double-clicking the model’s entry.

In the editor, add:

1. The encryption flag “`--encrypt`”
2. A space character
3. The absolute path to the model’s encryption key file you created (see [Generating a Model Encryption Key](/documentation/CoreML/generating-a-model-encryption-key)), between quotation marks

Press the Return key or click outside the editor to close it.

![Screenshot of an Xcode project’s Build Phases tab, showing a model’s compiler flags editor. The editor’s text reads: dash-dash-encrypt, space, and then the path to the model beginning with quotation mark dollar-sign S-R-C-root and then the path to the model file, ending in “dot M-L-model key”.](images/com.apple.coreml/media-3675192@2x.png)

### Load the model

At runtime, you load the encrypted model the same way you load any other built-in model by using its `load(completionHandler:completionHandler:)` type method. This method behaves similarly to the [`MLModel`](/documentation/CoreML/MLModel) type method [`loadContentsOfURL:configuration:completionHandler:`](/documentation/CoreML/MLModel/loadContentsOfURL:configuration:completionHandler:) and creates an instance of the model using the convenience class that Xcode generates.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)