Skip to content

Step 4. Initialize the SDK

Hive Axyl SDK initialization is the preparation step that the app goes through before it uses Hive Axyl SDK features. When the app starts, call initialization to prepare to use Hive Axyl features such as authentication, payment, and push. Initialization runs only once while the app is running, so do not call it more than once.

Initialization

Method

Initialize

Run initialization to prepare the Hive Axyl SDK runtime that is shared across the entire app. Call it only once when the app starts.

  1. Check the App ID you created in Create app information.
  2. Run CoreConfig.CreateBuilder with the App ID to create a CoreConfig object.
  3. Run initialization with HiveBootstrap.Initialize. You must use the assemble callback to register the authentication module (AddAuth), the token module (AddToken), and the secure storage module (AddSecureStorage).

Call parameters

Field name Type Required Description
config CoreConfig Required The CoreConfig object that contains the App ID. Create it with CoreConfig.CreateBuilder.
assemble Action<IHiveBuilder> Required The builder callback that registers the feature modules to use.

Call example

Because the Get started guide covers only login, logout, and account deletion, register only the authentication, token, and secure storage modules they require, and run initialization.

using Hive.Axyl.Core;
using Hive.Axyl.Core.Unity;   // HiveBootstrap
using Hive.Axyl.Auth;   // AddAuth, AddToken extensions
using Hive.Axyl.Storage;   // AddSecureStorage extension

// Call only once when the app starts (for example, MonoBehaviour.Start of the first scene).
var config = CoreConfig.CreateBuilder("{appId}").Build();

HiveBootstrap.Initialize(config, builder =>
{
    // Register the modules to use.
    builder
        .AddAuth()    // Account and authentication (IAuthService)
        .AddToken()   // Token issuance (ITokenService)
        .AddSecureStorage();  // Device secure storage (ISecureStorage)
});

Register the feature modules to use together in the assemble callback, the second argument of Initialize. The builder in the example above is the builder object that this callback passes. Because the Get started guide implements login, you must register the authentication module (AddAuth) and the token module (AddToken). AddToken is required to issue actual tokens using the authorizationCode received as the login result as input, so always register it together with the authentication module. In addition, register the secure storage module (AddSecureStorage) to clean up the tokens saved on the device during logout and account deletion.

Later, retrieve and use the registered modules with HiveCore.Resolve<IAuthService>() and HiveCore.Resolve<ITokenService>(). Because secure storage is not registered in some environments, retrieve it with HiveCore.TryResolve<ISecureStorage>(out var storage).

Register the secure storage module

AddSecureStorage registers the com.com2usplatform.hiveaxyl.storage module that you installed in Install modules. When you delete the tokens saved on the device in Log out and Delete an account, you retrieve and use this module with HiveCore.TryResolve<ISecureStorage>(out var storage), so register it together at initialization.

AddSecureStorage registers ISecureStorage only on platforms that have device secure storage. These are Android, iOS, macOS, and Windows player builds. Because it is not registered in the Unity Editor, HiveCore.Resolve<ISecureStorage>() throws RegistrationNotFoundException. For code that also runs in the Editor, first check whether it is registered with HiveCore.TryResolve<T>(), as shown below.

using Hive.Axyl.Core;
using Hive.Axyl.Storage;

if (HiveCore.TryResolve<ISecureStorage>(out var storage))
{
    // This platform can use secure storage.
}

Register multiple feature modules together

Call initialization only once while the app is running. An app that uses the authentication, payment, mailbox, service access control, and Analytics features together registers those modules together at initialization.

using Hive.Axyl.Core;
using Hive.Axyl.Core.Unity;   // HiveBootstrap
using Hive.Axyl.Auth;   // Authentication (AddAuth, AddToken extensions)
using Hive.Axyl.Storage;      // Device secure storage
using Hive.Axyl.Payments;     // Payment
using Hive.Axyl.Mailbox;      // Mailbox
using Hive.Axyl.ServiceAccess; // Service access control
using Hive.Axyl.Analytics;     // Analytics

// Call only once when the app starts (for example, MonoBehaviour.Start of the first scene).
var config = CoreConfig.CreateBuilder("{appId}").Build();

HiveBootstrap.Initialize(config, builder =>
{
    // Register the modules to use.
    builder
        .AddAuth()    // Account and authentication (IAuthService)
        .AddToken()   // Token issuance (ITokenService)
        .AddSecureStorage()  // Device secure storage (ISecureStorage)
        .AddPayments()  // Payment (IPaymentsService)
        .AddMailbox()   // Mailbox (IMailboxService)
        .AddServiceAccess() // Service access control (IServiceAccessService)
        .AddAnalytics();    // Analytics (IAnalyticsService)
});

Response data

No data is returned on success.

Response example

// On success, execution continues to the next line without exceptions.
IAuthService auth = HiveCore.Resolve<IAuthService>();
ITokenService token = HiveCore.Resolve<ITokenService>();

// Secure storage is not registered on some platforms, so check it with TryResolve.
bool hasStorage = HiveCore.TryResolve<ISecureStorage>(out var storage);

Response status

If calling HiveCore.Resolve<IAuthService>() and HiveCore.Resolve<ITokenService>() in your code returns the module objects without exceptions, initialization and module registration are complete. The returned module objects are IAuthService and ITokenService instances. Because secure storage is intentionally not registered in the Unity Editor, check it with TryResolve instead of Resolve.

A call to HiveBootstrap.Initialize throws exceptions under the following conditions.

  • ArgumentNullException if you pass null to config
  • InvalidOperationException if you call the initialization method again after initialization is already complete
  • DllNotFoundException if the native plugin fails to load on Windows. Check that you installed the Microsoft Visual C++ 2015-2022 Redistributable (x64) as described in Windows runtime prerequisites. The exception message includes a link to the installer.

Unlike a Failure in a method call result, these exceptions make the initial setup fail immediately. For common principles, including the missing-registration exception of Resolve<T>(), see Common error handling.

Next steps

To create an account to log in with, see Create an account.