# User Identity APIs in Mobile SDKs


This guide covers the user identity APIs that help you retrieve the following parameters persisted by the RudderStack [Android (Kotlin)]({{< ref "sources/event-streams/sdks/kotlin-sdk/" >}}) and [iOS (Swift)]({{< ref "sources/event-streams/sdks/swift-sdk/" >}}) SDKs:

- [User ID](#fetch-user-id)
- [Anonymous ID](#fetch-anonymous-id)
- [User traits](#fetch-user-traits)

## Fetch user ID

The Android (Kotlin) and iOS (Swift) SDKs assign a unique `userId` when an [`identify`]({{< ref "identify.md" >}}) event is triggered. This parameter uniquely identifies a user within the database after they log in or sign up.

The following sections highlight how to obtain the user's `userId` in Android (Kotlin) and iOS (Swift) SDKs.

### Android (Kotlin)

{{< tabs tabTotal="2" >}}
{{% tab tabName="Kotlin" %}}
```kotlin
val userId = analytics.userId
```
{{% /tab %}}
{{% tab tabName="Java" %}}
```java
String userId = analytics.getUserId();
```
{{% /tab %}}
{{< /tabs >}}

### iOS (Swift)

{{< tabs tabTotal="2" >}}
{{% tab tabName="Swift" %}}
```swift
let userId = analytics.userId
```
{{% /tab %}}
{{% tab tabName="Objective-C" %}}
```objectivec
NSString* userId = self.analytics.userId;
```
{{% /tab %}}
{{< /tabs >}}

The above snippets return the following information:

| Property name | Return type | Description |
| :----| :-----| :---|
| `userId`  | String? | The stored `userId` for the user. <br /><br />**Note**: The SDK returns `null` if the `Analytics` instance is shut down. |

## Fetch anonymous ID

`anonymousId` is a unique identifier automatically generated by the SDK as a **random UUID (Universally Unique Identifier)** to track unidentified users, for example, users before they log in or sign up. This ID persists across sessions and allows tracking user behavior before they are identified explicitly.

The following sections highlight how to obtain the user's `anonymousId` in Android (Kotlin) and iOS (Swift) SDKs.

### Android (Kotlin)

{{< tabs tabTotal="2" >}}
{{% tab tabName="Kotlin" %}}
```kotlin
val anonymousId = analytics.anonymousId
```
{{% /tab %}}
{{% tab tabName="Java" %}}
```java
String anonymousId = analytics.getAnonymousId();
```
{{% /tab %}}
{{< /tabs >}}

### iOS (Swift)

{{< tabs tabTotal="2" >}}
{{% tab tabName="Swift" %}}
```swift
let anonymousId = analytics.anonymousId
```
{{% /tab %}}
{{% tab tabName="Objective-C" %}}
```objectivec
NSString* anonymousId = self.analytics.anonymousId;
```
{{% /tab %}}
{{< /tabs >}}

The above snippets return the following information:

| Property name | Return type | Description |
| :----| :-----| :---|
| `anonymousId`  | String? | The stored `anonymousId` for the user. <br /><br />**Note**: The SDK returns `null` if the `Analytics` instance is shut down. |

## Fetch user traits

The Android (Kotlin) and iOS (Swift) SDKs use the `traits` object to store any additional user attributes, for example, name, email, custom properties, etc. associated with an `identify` event. These traits allow for richer user profiling and segmentation.

The following sections highlight how to obtain the user traits in Android (Kotlin) and iOS (Swift) SDKs.

### Android (Kotlin)

{{< tabs tabTotal="2" >}}
{{% tab tabName="Kotlin" %}}
```kotlin
val traits = analytics.traits
```
{{% /tab %}}
{{% tab tabName="Java" %}}
```java
Map<String, Object> traits = analytics.getTraits();
```
{{% /tab %}}
{{< /tabs >}}

### iOS (Swift)

{{< tabs tabTotal="2" >}}
{{% tab tabName="Swift" %}}
```swift
let traits = analytics.traits
```
{{% /tab %}}
{{% tab tabName="Objective-C" %}}
```objectivec
NSDictionary *traits = self.analytics.traits;
```
{{% /tab %}}
{{< /tabs >}}

The above snippets return the following information:

| Property name | Return type | Description |
| :----| :-----| :---|
| `traits`  | JsonObject? / [String: Any]? | A structured object containing the user's traits. <br /><br />Note that:<br /><br /><ul><li>The SDK returns `null` if the `Analytics` instance is shut down.</li><li>The `traits` return type in Java is `Map<String, Object>`.</li><li>The `traits` return type in iOS (Swift) is `[String: Any]?`.</li></ul> |
