---
title: Unity (iOS)
slug: unity-ios
createdAt: 2022-09-05T16:48:08.000Z
---

This guide is for anyone who wishes to integrate the Enefits Unity SDK for iOS into the Unity project.

We highly recommend reviewing the General Usage guide to familiarise yourself with general SDK workflow concepts before continuing.

You will need to generate an API key with your Enefits account before continuing with integration.

To obtain an API key, simply register at [https://x.enefits.co/](https://x.enefits.co/), and go to Developer in the account menu in the top right section. From there, generate an API key and save this for SDK initialization.

::Image[]{src="https://api.archbee.com/api/optimize/DWsWVx1Q7dvTK3kHl-Tee/dd3KiCpWNUSjvCl_hV_rZ_screenshot-2022-09-05-at-92346-am.png" size="36" width="624" height="1032" position="center" showCaption="false"}

We only show this once, so if you lose this API Key, you’ll have to re-generate it from the Developer section.

## Run the Demo

To run a live demo, download [EnefitsSDKDemoApp](https://github.com/Enefits/enefits-unity-ios-sdk/tree/main/EnefitsSDKDemoApp).

Open it with Unity and provide your own API key where requested.

Also, note that EnefitsSDKDemoApp requires an iOS device to work. It will not work in the Unity Editor Or the iOS Simulator.

Make sure you have ios Build options support available in Unity.

![](https://api.archbee.com/api/optimize/DWsWVx1Q7dvTK3kHl-Tee/zqwbWGzKdUM7yeb7zuYDv_screenshot-2022-09-05-at-91453-am.png)

Also, make sure you have XCode OR XCode Command Line Tools installed.

Go to Unity -> File -> Build Settings.

Make sure 'iOS' option is selected.

Select option 'Build', it will ask for a location where unity will generate iOS Project.

Select a location, then it will generate iOS Project at a specified location.

Open generated XCode Project File with XCode.

![](https://api.archbee.com/api/optimize/DWsWVx1Q7dvTK3kHl-Tee/QrGJnUWAZj-PxVrSZZvYf_screenshot-2022-09-05-at-91645-am.png)

Make sure you have enabled XCode Project with a valid developer profile & signing certificates and valid Application Bundle Id.

![](https://api.archbee.com/api/optimize/DWsWVx1Q7dvTK3kHl-Tee/3Erm5JvfF_yBUg8-VFOVM_screenshot-2022-09-05-at-91813-am.png)

Change Bundle Identifier, version and build number as per your requirement.

You can either run EnefitsSDKDemoApp on the device or create a generic device build.

![](https://api.archbee.com/api/optimize/DWsWVx1Q7dvTK3kHl-Tee/bwIKNagm4vi6ar45_GcHz_screenshot-2022-09-05-at-91831-am.png)

Select a valid provision profile and a valid signing certificate.

![](https://api.archbee.com/api/optimize/DWsWVx1Q7dvTK3kHl-Tee/xj_lgRdAM5VGpMj8YMo5O_screenshot-2022-09-05-at-91946-am.png)

## Download Enefits SDK Unity Package

The EnefitsSDK Unity iOS package can be downloaded from [this link.](https://github.com/Enefits/enefits-unity-ios-sdk/tree/main/Unity-Package)


## Import Enefits SDK Unity Package

Select Project layout. Go to the Assets folder. Right-click and select Import Package -> Custom Package.


::Image[]{src="https://api.archbee.com/api/optimize/DWsWVx1Q7dvTK3kHl-Tee/-d3m0mr_OsPGWNWhUPUra_screenshot-2022-09-05-at-100521-pm.png" size="52" width="1082" height="912" position="center" showCaption="false"}

Select a downloaded Enefits package and choose 'Open'.

Make sure all components are selected from the package and then click on 'Import'.

::Image[]{src="https://api.archbee.com/api/optimize/DWsWVx1Q7dvTK3kHl-Tee/4ygNgv-elkmin2qQ0nBg4_screenshot-2022-09-05-at-95408-pm.png" size="58" width="924" height="1380" position="center" showCaption="false"}

## SDK Setup

Please make sure to call any SDK method inside 'UNITY\_IOS && !UNITY\_EDITOR' block, as it is only available for iOS:

:::BlockQuote
\#if UNITY\_IOS && !UNITY\_EDITOR
&#x20;   // call SDK method here
\#endif
:::


Initialize The SDK
------------------

```csharp
#if UNITY_IOS && !UNITY_EDITOR
    EnefitsNative.EnefitsSDK_initializeSDK("<App_Name>","<API_Key>",completionHandler);
#endif
```


\<App\_Name> - string type - describes your unity ios application name

\<API\_key> - string type - API key provided to you during the registration at  [https://x.enefits.co/](https://x.enefits.co/).

completionHandler can be defined as:



```csharp
[MonoPInvokeCallback ( typeof ( Action<bool,string> ) )]
public static void completionHandler ( bool status, string message ) {

}
```

status => true/false SDK init success/failed

message => error / message from the Enefits SDK

## Connect Wallet

Display a button or call-to-action (for example: 'Connect Wallet') which when tapped will prompt the user to connect a wallet.

After the user selects a wallet and initiates a session, the returned address will be available to the Enefits SDK to check NFTs and any offers the user is eligible for.

This function will open a view with a list of supported providers

Enefits SDK fires the callback after successfully connecting with a blockchain account.

```csharp
#if UNITY_IOS && !UNITY_EDITOR
    EnefitsNative.EnefitsSDK_Present(completionHandler);
#endif
```

where completionHandler can be defined as:

```csharp
[MonoPInvokeCallback ( typeof ( Action<bool,string> ) )]
public static void completionHandler ( bool status, string message ) {

}
```

status = true => Connecting with wallet

status = false => Already connected or not initialized

message => string message from the Enefits SDK

To get callback after wallet connect:

```csharp
#if UNITY_IOS && !UNITY_EDITOR     
    EnefitsNative.Enefits_isConnectedWithWallet(walletcompletionHandler);
#endif  
```

walletcompletionHandler can be defined as:

```csharp
[MonoPInvokeCallback ( typeof ( Action<bool> ) )]
public static void walletcompletionHandler ( bool status ) {

}
```

status = true => Conected with wallet successfully.

status = false => failed to connect wallet.&#x20;

## Get All Enefits Offers

This method returns all Offers that the user is eligible for based on the address they provided when they connected their wallet.

```csharp
#if UNITY_IOS && !UNITY_EDITOR
    EnefitsNative.EnefitsSDK_getOffers(completionHandler);
#endif
```

completionHandler can be defined as:

```csharp
[MonoPInvokeCallback ( typeof ( Action<bool,string> ) )]
public static void completionHandler ( bool status, string message ) {

}
```

status = true => able to fetch offers successfully.

status = false => failed to get offers.

message => if status = true then message describes offers list (JSON string), if status = false, then message describes error info.

## Helper Functions

Use the functions provided below to create tighter integration with the Enefits SDK and manage user connectivity states.

### To Check if SDK init success or failed:

```csharp
#if UNITY_IOS && !UNITY_EDITOR     
    if(EnefitsNative.Enefits_isInitComplete()) {
        // sdk init success
    } else {
        // sdk init failed
    }
#endif
```

### To Check if Enefits SDK was able to connect to a blockchain account.

```csharp
#if UNITY_IOS && !UNITY_EDITOR     
    if(EnefitsNative.Enefits_isAccountConnected()) {
        // wallet account connected
    } else {
        // failed to connect wallet account
    }
#endif
```

### Get the address of the connected account.

```csharp
#if UNITY_IOS && !UNITY_EDITOR
    EnefitsNative.Enefits_getConnectAccount(getConnectAccountcompletionHandler);
#endif
```

where getConnectAccountcompletionHandler can be defined as:

```csharp
[MonoPInvokeCallback ( typeof ( Action<string> ) )]
public static void getConnectAccountcompletionHandler ( string account, string blockChainInfo ) {

}
```

account => Connected account information.

blockChainInfo => Wallet information (JSON string)

### Get the blockchain info for the connected account.

```csharp
#if UNITY_IOS && !UNITY_EDITOR
    EnefitsNative.Enefits_getChainData(getChainDatacompletionHandler);
#endif
```

where getChainDatacompletionHandler can be defined as:

```csharp
[MonoPInvokeCallback ( typeof ( Action<string> ) )]
public static void getChainDatacompletionHandler ( string data ) {

}
```

data => blockchain info for the connected account (JSON string).

### Disconnect from the blockchain account.

```csharp
#if UNITY_IOS && !UNITY_EDITOR
    EnefitsNative.Enefits_disconnect(completionHandler);
#endif
```

where completionHandler can be defined as:

```csharp
[MonoPInvokeCallback ( typeof ( Action<bool,string> ) )]
public static void completionHandler ( bool status, string message ) {

}
```

status = true/fase => able to disconnect from wallet successfully / failed to disconnect from wallet.

message =>  if status = false then message explains reason for failure while disconnecting.

