# iOS

## Overview

The Blitzllama iOS SDK enables you to deliver in-product surveys directly inside your iOS app. By integrating the SDK, you can collect targeted user feedback at critical moments in the user journey.

**Key Features:**

* Quickly embed surveys via triggers you define in your app.
* Identify and track users to ensure they don’t receive duplicate surveys unnecessarily.
* Personalize surveys with user attributes (optional).
* Localize surveys by setting different languages (optional).

**Requirements:**

* iOS 11 and above
* Access to the Blitzllama dashboard (to retrieve your API key and manage triggers).

## Getting Started

### Before You Begin

**Obtain Your API Key:**\
Log in to the Blitzllama dashboard, navigate to the Connections Tab, click iOS, and find your `API_KEY`.

## Quick Start (5 Minutes)

### 1. SDK Installation

#### Option A: Cocoapods (Recommended)

1. Add the SDK to your Podfile:

```bash
pod 'Blitzllama-ios', '1.6.29'
```

2. Install the pods:

```bash
pod install
```

#### Option B: Swift Package Manager (SPM)

* In Xcode, go to File > Add Packages.
* Use the [Blitzllama iOS SDK git](https://github.com/Blitzllama/ios-releases) repository.
* Select the latest version from he master branch and add it to your project targets.

### 2. Initializing the SDK

In your `AppDelegate` (or `SceneDelegate` if using newer lifecycle):

```swift
import BlitzLlamaSDK
import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        //initialisation Blitzllama
        BlitzLlamaSDKController.getSDKManager.setBlitzLlamaAPIKey("API_KEY")
        return true
    }
}

```

### 3. Create a User

Ensure you create a user with a unique user ID before triggering surveys to avoid duplicate survey prompts:

```swift
BlitzLlamaSDKController.getSDKManager.createUser("user_id")
```

### 4. Trigger a Survey

Add a trigger in your app (e.g., on a button press or screen load) to display surveys:

```swift
BlitzLlamaSDKController.getSDKManager.fetchSurvey(triggerName: "triggerName")
```

Make sure the trigger name matches the configuration on the Blitzllama dashboard.

You’re now ready to collect targeted user feedback with Blitzllama’s in-app surveys! 🚀

***

## Triggers and Event Properties

### **What are Triggers?**

Triggers are events in your app (e.g., button presses, screen views) that prompt a survey if configured in the Blitzllama dashboard.

### **Adding Trigger Properties (Optional)**

```swift
// Define your event properties
let eventProperties: [String: Any] = [
    "eventKey1": "eventValue1",
    "eventKey2": "eventValue2"
]

// Trigger the survey with the event properties
BlitzLlamaSDKController.getSDKManager().fetchSurvey(triggerName: "triggerName", properties: eventProperties)

```

**Why Use Trigger Properties?**

* Filter users more precisely in the Blitzllama dashboard.
* Understand user context when surveys are triggered.

{% hint style="success" %}
You can also use **backend events** to launch in-app surveys. Check out <https://documentation.blitzllama.com/connections/backend-trigger> for more details.
{% endhint %}

## Personalizing and Localizing Surveys <a href="#personalizing-and-localizing-surveys" id="personalizing-and-localizing-surveys"></a>

### Setting User Attributes

Attributes help you segment and target specific user groups. Allowed data types are `"string"`, `"number"`, `"date"`, `"boolean"`.

```swift
BlitzLlamaSDKController.getSDKManager.updateUserAttributes("attribute_name", attributeValue: "attribute_value", dataType: "string")  
```

### Set User Email or Name (Optional)

Providing the user’s email or name is optional but can help personalize surveys.

```swift
BlitzLlamaSDKController.getSDKManager.updateUserEmail("user_email")
BlitzLlamaSDKController.getSDKManager.updateUserName("user_name")
```

### Setting Survey Language

Set the survey language using an  [ISO 639-1 codes](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) (e.g., `"en"` for English).

```swift
BlitzLlamaSDKController.getSDKManager.setLanguageCode("en")
```

**Why Set Survey Language?**

* Improve user experience by displaying surveys in the user's native language.
* Better localization for multi-lingual apps.

## User Management

### Logout User

When a user logs out, call the logout method to clear their session:

```swift
BlitzLlamaSDKController.getSDKManager.logout()
```

**Why Logout?**

* Prevents mixing user data between sessions.
* Ensures a clean state when switching users.

## FAQ & Troubleshooting

* **Survey not showing up?**
  * Ensure `createUser` is called before triggering an event.
  * Check that the trigger name matches exactly what’s configured in the Blitzllama dashboard.
* **Survey not showing on the first screen?**
  * This might be due to timing issues. Consider delaying the trigger call until after `createUser` completes.
* **Switching users?**
  * Call `logout()` before creating a new user to avoid mixing sessions.
* **How to create triggers?**
  * Add a new `trigger_name` in your app. If it doesn’t exist, it’s automatically created and will appear on the dashboard.
  * Alternatively, create the trigger on the Blitzllama dashboard first, then use the same `trigger_name` in the `fetchSurvey` call.
* **Need more help?**
  * Reach out via support chat or email `tech@blitzllama.com`.

### Next Steps

* Start by adding 5 triggers for the most critical user actions in your app.
* Create your first survey on the Blitzllama dashboard.
* Customize the UI/UX of the survey on the Blitzllama dashboard.
