# Native Android & Apple UI

Initialize AppPortal before Android Application base construction or Apple application startup, then register the existing client for DI.

Source: https://appportal.io/docs/sdks/dotnet/native-mobile

Use this guide when your C# app builds its UI with native Android or iOS frameworks.
MAUI and Uno use the same native initialization boundary; their [framework guides](https://appportal.io/docs/sdks/dotnet)
show how to register the resulting instance in DI.

## 1. Install in the executable head

Install the public release in the executable app project:

```bash
dotnet add package AppPortal.Telemetry --version "3.0.384"
```

If AppPortal supplied a different release, use its package source and exact version.
See [version notes](https://appportal.io/docs/getting-started/documentation-versions) before updating an existing app.

The target framework selects the matching runtime. Supported .NET Apple targets include iOS,
Mac Catalyst, macOS, and tvOS; this is not a .NET watchOS package.
Shared libraries that call the telemetry API also reference `AppPortal.Telemetry`.

## 2. Initialize before application startup

Import `AppPortal` and initialize once in the executable head before constructing the application
or UI host. The native SDK owns storage, crash hooks, lifecycle and startup timing.

### Android

In your Android `Application` subclass:

```csharp
public MainApplication(IntPtr handle, Android.Runtime.JniHandleOwnership ownership)
    : base(AppPortalSdk.CreateBuilder("<app-credential>").Start(handle), ownership)
{
}
```

`Start(handle)` uses the existing native reference and returns it unchanged. Initialization runs
before the managed Application base constructor; it does not wait for `OnCreate` or an Activity.

Keep initialization in the native Application even when the app can launch only to do background
work. Such a launch may never create an Activity, a MAUI/Uno host, or a Prism container. That
background code can use the SDK directly; do not create a second runtime or wait for UI registration.

The automatic cold-start clock begins here, before SDK runtime/storage recovery, and ends at the
first Activity resume. It includes work in the Application and the delay before MainActivity is
created. No application stopwatch, lifecycle forwarding, or startup-complete callback is needed.

### iOS, Mac Catalyst, and tvOS

In the executable's `Main`, before UIKit creates the app and delegate:

```csharp
private static void Main(string[] args)
{
    AppPortalSdk.CreateBuilder("<app-credential>").Start();
    UIKit.UIApplication.Main(args, null, typeof(AppDelegate));
}
```

Keep the rest of your launch logic. A macOS AppKit head likewise initializes in its executable
entry point before creating/running its application. Do not wait for launch-delegate callbacks.

If the application uses DI, register the already-current instance afterward:

```csharp
using Microsoft.Extensions.DependencyInjection;

services.AddAppPortal();
```

This exposes `AppPortalSdk.Current` as `IAppPortalClient`; it does not initialize, replace or own
the runtime. Shared services can receive the interface and tests can supply a mock.

## 3. Connect consent and find your session

The client begins with collection pending unless a saved consent decision exists.
After the user opts in through your app's consent UI:

```csharp
AppPortalSdk.GrantConsent();
```

Run the app, background and resume it, then [find the session in the dashboard](https://appportal.io/docs/getting-started/first-event#5-find-your-session).
The native runtime provides lifecycle context without a custom event for each transition.
Reset and revocation immediately restrict capture and delivery admission while the native owner
finishes durable cleanup. A later grant waits behind that cleanup; see the persistence and
process-termination boundaries in the consent guide below.
For a workforce app with an approved initial collection policy, see
[consent configuration](https://appportal.io/docs/sdks/dotnet/telemetry#connect-your-consent-ui).

## Instrument your app's behavior

The native runtime observes application lifecycle. Add a stable screen name at your navigation
boundary with `TrackScreen`; do not send navigation arguments or personal data as screen names.
Shared C# code can use `AppPortalSdk` or receive `IAppPortalClient` from your app's DI setup.

Continue with [logs, handled exceptions, custom events, and timings](https://appportal.io/docs/sdks/dotnet/telemetry).
For a test build, [verify a known event and crash after restart](https://appportal.io/docs/getting-started/verify-first-value).
