# Unity as a Library on iOS

> Understand how the Unity as a Library feature embeds the Unity Runtime Library in a native iOS application.

The Unity as a Library feature integrates the Unity Runtime Library into native iOS applications.

You can use this feature to include Unity-powered capabilities, such as 2D and 3D real-time rendering, augmented reality (AR) experiences, 3D model interaction, or 2D mini-games, in your native application. The Unity Runtime Library exposes controls to manage when and how to load, activate, and unload content in your native application.

The following sections describe the Xcode project structure that Unity generates, the methods that control the Unity runtime, and the known limitations of the feature.

## Xcode project structure and integration

To use Unity as a Library for iOS, first build your Xcode project from Unity as you normally would. For more information, refer to [Build an iOS application](/engine/6000.7/manual/platform-specific/iphone/ios-building-and-delivering/build-process.md).

Every Unity iOS Objective-C Xcode project has the structure described in [Structure of a Unity Xcode Objective-C project type](/engine/6000.7/manual/platform-specific/iphone/ios-developing/structure-of-xcode-project.md):

* A library part in the **UnityFramework** target that contains source files (such as `UnityFramework/UnityFramework.h`), plug-ins, and dependent frameworks. Building this target in Xcode produces the `UnityFramework.framework` bundle. Unity doesn't place a prebuilt `.framework` in the export folder.
* A thin launcher part in the **Unity-iPhone** target that includes app representation data and runs the library. The **Unity-iPhone** target has a single dependency on the **UnityFramework** target.

> **Note:**
>
> The [Swift Xcode project type](/engine/6000.7/manual/platform-specific/iphone/ios-developing/ios-swift-xcode-project-type/ios-migrate-swift-project-type.md) doesn't support Unity as a Library.

To integrate Unity into another Xcode project, combine both Xcode projects (the native one and the one Unity generates) into a single Xcode workspace. Then add the built `UnityFramework.framework` to **Frameworks, Libraries, and Embedded Content** (or **Embedded Binaries**) for your native application's target. After you do this, you can use the `UnityFramework` class to control the Unity runtime.

For example projects and plug-ins that demonstrate how to integrate Unity into an Xcode project, refer to the [`uaal-example` repository](https://github.com/Unity-Technologies/uaal-example/blob/master/docs/ios.md) (Unity Technologies on GitHub).

## Methods to control the Unity runtime

After you load `UnityFramework.framework` in the host app, control the Unity runtime through the `UnityFramework` Objective-C class. The class is declared in `UnityFramework/UnityFramework.h` and is the principal class of the framework bundle.

The following table lists the methods of the `UnityFramework` class:

| **Method**                                                                                                     | **Description**                                                                                                                                                                                                                                                                                                                          |
| -------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `+ (UnityFramework*)getInstance;`                                                                              | Returns the singleton instance of `UnityFramework`.                                                                                                                                                                                                                                                                                      |
| `- (UnityAppController*)appController;`                                                                        | Returns the `UnityAppController` subclass of `UIApplicationDelegate`. This is the root Unity class in native code, and can access the app's view-related objects, such as `UIView`, `UIViewController` instances, `CADisplayLink`, or `DisplayConnection`.                                                                               |
| `- (void)setDataBundleId:(const char*)bundleId;`                                                               | Sets the bundle where the Unity runtime looks for the `Data` folder. Call this method before `runUIApplicationMainWithArgc` or `runEmbeddedWithArgc`. For more information, refer to [Structure of a Unity Xcode Objective-C project type](/engine/6000.7/manual/platform-specific/iphone/ios-developing/structure-of-xcode-project.md). |
| `- (void)runUIApplicationMainWithArgc:(int)argc argv:(char*[])argv;`                                           | Runs Unity from the `main` method when no other views exist. This is the default way to run Unity.                                                                                                                                                                                                                                       |
| `- (void)runEmbeddedWithArgc:(int)argc argv:(char*[])argv appLaunchOpts:(NSDictionary*)appLaunchOpts;`         | Runs Unity when other views already exist.                                                                                                                                                                                                                                                                                               |
| `- (void)unloadApplication;`                                                                                   | Unloads Unity and triggers a callback to `UnityFrameworkListener` after the unload completes. Unity releases most of the memory it occupies, but not all of it. You can run Unity again.                                                                                                                                                 |
| `- (void)registerFrameworkListener:(id<UnityFrameworkListener>)obj;`                                           | Registers a listener object that receives callbacks for `UnityFramework` lifecycle events.                                                                                                                                                                                                                                               |
| `- (void)unregisterFrameworkListener:(id<UnityFrameworkListener>)obj;`                                         | Unregisters a listener object.                                                                                                                                                                                                                                                                                                           |
| `- (void)showUnityWindow;`                                                                                     | Shows a Unity view that's already running, while a non-Unity view is visible.                                                                                                                                                                                                                                                            |
| `- (void)pause:(bool)pause;`                                                                                   | Pauses Unity.                                                                                                                                                                                                                                                                                                                            |
| `- (void)setExecuteHeader:(const MachHeader*)header;`                                                          | Sets the executable header that CrashReporter uses. Call this method before you run Unity.                                                                                                                                                                                                                                               |
| `- (void)sendMessageToGOWithName:(const char*)goName functionName:(const char*)name message:(const char*)msg;` | Acts as a proxy to `UnitySendMessage`. Finds a GameObject by name and calls `functionName` with a single-string message parameter. For more information, refer to [Native plug-in development for iOS](/engine/6000.7/manual/platform-specific/iphone/ios-developing/plugins-for-ios.md).                                                |
| `- (void)quitApplication:(int)exitCode;`                                                                       | Unloads Unity completely and triggers a callback to `UnityFrameworkListener` when Unity quits. Unity releases all memory.**Note**: You can't run Unity again in the same process after this call. You can set `quitHandler` on `AppController` to override the default process termination.                                              |

## Limitations

Unity doesn't control the runtime lifecycle, so Unity as a Library might not work in every scenario. Known limitations include:

* Unity as a Library supports only full-screen rendering, not rendering to part of the screen.

* You can't load more than one instance of the Unity runtime.

* You might need to adapt third-party plug-ins (both [native](/engine/6000.7/manual/scripting/compilation-and-code-reload/plug-ins/native.md) and [managed](/engine/6000.7/manual/scripting/compilation-and-code-reload/plug-ins/managed.md)) to work with the Unity runtime.

* You can't adjust the device's status bar visibility through the [iOS Player settings](/engine/6000.7/manual/platform-specific/iphone/getting-started/class-player-settings-ios.md#Status) when you use Unity as a Library in a native iOS application. To hide the status bar, use one of the following options:

  * Set the [`UIStatusBarHidden`](https://developer.apple.com/documentation/bundleresources/information_property_list/uistatusbarhidden?language=objc) or [`UIViewControllerBasedStatusBarAppearance`](https://developer.apple.com/documentation/bundleresources/information_property_list/uiviewcontrollerbasedstatusbarappearance?language=objc) key in the `info.plist` file of your project.
  * Set the view controller property [`prefersStatusBarHidden`](https://developer.apple.com/documentation/swiftui/uihostingcontroller/prefersstatusbarhidden/) and call the [`setNeedsStatusBarAppearanceUpdate`](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621354-setneedsstatusbarappearanceupdat?language=objc) method.
  * Set the view controller property [`childViewControllerForStatusBarHidden`](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621451-childviewcontrollerforstatusbarh?language=objc).

## Additional resources

* [Use Unity as a Library in other applications](/engine/6000.7/manual/platform-specific/cross-platform-features/unityasa-library.md)
* [Structure of a Unity Xcode Objective-C project type](/engine/6000.7/manual/platform-specific/iphone/ios-developing/structure-of-xcode-project.md)
* [Build an iOS application](/engine/6000.7/manual/platform-specific/iphone/ios-building-and-delivering/build-process.md)
* [Introduction to native plug-ins on iOS](/engine/6000.7/manual/platform-specific/iphone/ios-developing/plugins-for-ios/ios-introduction-to-plugins.md)
* [Migrate to a Swift Xcode project type](/engine/6000.7/manual/platform-specific/iphone/ios-developing/ios-swift-xcode-project-type/ios-migrate-swift-project-type.md)
