# iOS SDK

## Anecdote iOS SDK

This package will help you integrate Anecdote into your app.

### Installation

#### Swift Package Manager

Currently we only support the [Swift Package Manager](https://swift.org/package-manager/).

Once you have your Swift package set up, adding Anecdote as a dependency is as easy as adding it to the `dependencies` value of your `Package.swift` or the Package list in Xcode.

```
dependencies: [
    .package(url: "https://github.com/Anecdote-AI/anecdote-ios-sdk", .upToNextMajor(from: "0.1.0"))
]
```

Then you'll want to depend on the `Anecdote` target:

```
.product(name: "Anecdote", package: "Anecdote")
```

### Integration

You can create an instance of Anecdote object by providing your environment ID and the zone and use that instance moving forward.

```
let anecdote = Anecdote(envID: <#ENV_ID#>, zone: <#Zone#>)
```

### User Management

User attributes and event tracking are the main use cases of the SDK and `user` is an optional property of Anecdote. `User` has a failable initializer because it has a requirement of a non-empty String to be passed to identify a user. The user object is persisted locally.

#### Create and set the user for the first time:

```
anecdote.user = User(id: <#UserID#>, email: <#Email#>, attributes: <#Additional Attributes#>)

// Or just this as `email` and `attributes` have default values
anecdote.user = User(id: <#UserID#>)
```

#### Setting attributes

User object provides `subscript` to easily set attributes and setting any attribute to nil removes it:

```
anecdote.user?["city"] = <#City#> 
```

**Email has a dedicated property so either way of setting it is the same:**

```
anecdote.user?.email = <#Email#>

// Same as
anecdote.user?["email"] = <#Email#>
```

**Language also has a dedicated property and can be set either by using the provided enum or by providing an arbitrary string:**

```
anecdote.user?.language = <#LanguageEnum#>
anecdote.user?["language"] = <#LanguageString#>
```

#### Logout or removing the user

Just set the `user` property of `anecdote` to nil:

```
anecdote.user = nil
```

### Event Tracking

There times where you need to track specific events or actions the user takes which would be stored locally. All you have to do is just provide a string value for the `eventName` which would be trimmed (empty or whitespace-only strings are ignored).

```
anecdote.track(<#EventName#>)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.anecdoteai.com/docs/integrations/ios-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
