SDK docs 0.1.0 preview
ContentsConventions
0.1.0 preview. You install 0.1.0 from Maven Central and NGA's public repositories. Every 0.x version is a preview under the NGA SDK License, and the API can change until 1.0. Questions: hi@nga.network.

Reference

Conventions

View as Markdown

The rules every call follows on every platform, and how names map between Kotlin, Java, Swift, Objective-C and C#.

Threads #

  • Any thread in. Every public method can be called from any thread, and none blocks it.
  • Main thread out. Every callback and listener is delivered on the main thread (Android, iOS) or Unity's game thread (C#), always asynchronously, never inside the call that caused it, even when the answer is already known.
  • Unity on Android. Unity pauses while NGA's full-screen ad is in front, so that show's callbacks arrive after the ad closes, in their original order, in the first frame after Unity resumes. On iOS they arrive while the ad is on screen. See Pause and resume.
  • Swift 6 language mode. The examples use Swift 5 mode, the default for existing projects. In Swift 6 mode, a main-actor type (a view controller, your app delegate, a @MainActor class) must declare NGA's protocols, and MAX's and LevelPlay's delegates, @preconcurrency: final class GameOverViewController: UIViewController, @preconcurrency NGAAdLoadCallback, @preconcurrency NGAAdShowListener. The protocols are not actor-isolated, but the SDK calls them only on the main thread.
  • Exceptions in callbacks. The SDK does not catch exceptions your callbacks throw. In Unity they are logged by Unity's own loop, as any exception in a script is.

Callback ownership #

The SDK holds every callback strongly (so an inline lambda or block is never collected before it fires) and releases it at a fixed point:

Callback Released
NGAAdLoadCallback after onFailed; or when its ad is used, expires or fails; and never later than the ad's expiry (30 minutes for a cross-promo ad)
NGAAdShowListener after onClosed or onShowFailed
NGAListener when a later initialize with the same key passes another, non-null listener

A screen registered as a load callback can therefore stay in memory until its ad expires. Either register an app-wide object for loads and pass the screen only as the show listener, or guard callbacks that may arrive after the screen has gone, as the guide's examples do.

Null arguments #

A null from Java or C#, or nil from Objective-C, never throws. It gets the error that call already has for a bad value, such as INVALID_PLACEMENT / placement.format for a null placement id; a setter with nothing to report logs a warning and changes nothing.

Names in each language #

Types are NGA-prefixed and spelled the same on every platform.

Kotlin Java Swift Objective-C C#
Package or module network.nga.sdk network.nga.sdk import NGASDK @import NGASDK; using NGA;
Entry point NGASDK.loadAd(…) NGASDK.loadAd(…) (static) NGASDK.loadAd(…) [NGASDK loadAd:callback:] NGASDK.LoadAd(…)
A property error.retryable error.getRetryable() error.retryable error.retryable error.Retryable
A ready check ad.isReady() ad.isReady() ad.isReady (property) ad.isReady ad.IsReady()
An enum case NGAErrorCode.NO_FILL NGAErrorCode.NO_FILL .noFill NGAErrorCodeNoFill NGAErrorCode.NoFill
A nullable wait retryAfterMillis: Long? Long getRetryAfterMillis() retryAfterMillis: Int64? NSNumber *retryAfterMillis long? RetryAfterMillis
  • Kotlin properties appear to Java as getters (granted becomes getGranted()); every method on NGASDK is @JvmStatic.
  • Swift exports everything to Objective-C; the selectors are on each reference page.
  • C# uses PascalCase members in namespace NGA. Interfaces keep the NGA name with no I prefix. A mediation network keeps its own spelling: NGAAdRevenueMediation.AdMob, Max, LevelPlay.
  • No coroutines, Flows, async functions or Tasks. Every asynchronous answer is a callback.

Optional methods #

Some callback methods are optional; the reference marks them.

  • Kotlin interfaces give them empty default bodies, which Java can leave out as well.
  • Swift and Objective-C protocols mark them optional.
  • C# interfaces declare every member; implement the optional ones as empty methods.

Enum integers #

Every enum has the same cases and the same integers on every platform, and NGAErrorCode's integers are the error table's. Wherever an enum has a safe default (UNKNOWN, NONE or the least permissive case), it is 0. See Enums.

Values the SDK creates #

NGAError, NGAResult, NGAInitResult, NGARewardResult, NGAImpression and NGAAd come from the SDK; you never need to create one, except in your own tests, where Testing your own code says which you can.

Every callback for one ad receives the same NGAAd object, so you can compare by identity (=== in Kotlin and Swift) or by opportunityId. In C#, NGAAd is a sealed class with one instance per ad, and ==, Equals and GetHashCode compare OpportunityId.

Java and Objective-C #

The guide's examples are in C#, Kotlin and Swift. The same calls from Java and Objective-C:

Android (Java)

GameApplication.java
import android.app.Application;
import android.util.Log;

import network.nga.sdk.NGAAd;
import network.nga.sdk.NGAAdLoadCallback;
import network.nga.sdk.NGAConfig;
import network.nga.sdk.NGAConsent;
import network.nga.sdk.NGAError;
import network.nga.sdk.NGAInitResult;
import network.nga.sdk.NGAListener;
import network.nga.sdk.NGASDK;

public class GameApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        NGAConfig config = new NGAConfig.Builder("app_YOURAPPKEY", NGAConsent.unknown()).build();
        NGASDK.initialize(this, config, new NGAListener() {
            @Override
            public void onInitialized(NGAInitResult result) {
                Log.i("Game", "NGA ready: " + result.getSuccess());
            }
            // onError and onAdClosedAfterRestart are optional.
        });
        NGASDK.loadAd("plc_example_interstitial", new NGAAdLoadCallback() {
            @Override
            public void onLoaded(NGAAd ad) {
                Log.i("Game", "loaded " + ad.getOpportunityId());
            }

            @Override
            public void onFailed(String placementId, NGAError error) {
                Log.i("Game", "no ad: " + error.getCode() + " / " + error.getDetail());
            }
            // onExpired is optional.
        });
    }
}

iOS (Objective-C)

GameAppDelegate.m
@import UIKit;
@import NGASDK;

@interface GameAppDelegate : UIResponder <UIApplicationDelegate, NGAListener, NGAAdLoadCallback>
@end

@implementation GameAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NGAConfig *config = [[[NGAConfigBuilder alloc] initWithAppKey:@"app_YOURAPPKEY" consent:[NGAConsent unknown]] build];
    [NGASDK initializeWithConfig:config listener:self];
    [NGASDK loadAd:@"plc_example_interstitial" callback:self];
    return YES;
}

// NGAListener; onError: and onAdClosedAfterRestartWithPlacementId:opportunityId:rewardGranted: are optional.
- (void)onInitialized:(NGAInitResult *)result {
    NSLog(@"NGA ready: %d", result.success);
}

// NGAAdLoadCallback; onExpired: is optional.
- (void)onLoaded:(id<NGAAd>)ad {
    NSLog(@"loaded %@", ad.opportunityId);
}

- (void)onFailedWithPlacementId:(NSString *)placementId error:(NGAError *)error {
    NSLog(@"no ad: %ld / %@", (long)error.code, error.detail);
}

@end