# iOS SDK Setup

## 1. Requirements

{% hint style="info" %}

#### Can be used with other push solutions!

To use FlareLane with other push solutions, you must follow some additional steps. Refer to [ios-disable-swizzling](https://guide.flarelane.com/guide-for-developers/mobile-sdk-setup/additional-setup/ios-disable-swizzling "mention") for details.
{% endhint %}

* [Sign up](https://console.flarelane.com) to FlareLane and create a project

***

## 2. Credentials Setup

Follow this guide below:

* [ios-apns-setup](https://guide.flarelane.com/guide-for-developers/mobile-sdk-setup/additional-setup/ios-apns-setup "mention")

***

## 3. Xcode Setup

Go to `Signing & Capabilities` and click `+ Capability` and add `Push Notifications`

<figure><img src="https://4151189289-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSJ35x4PFzaneH3SrvcBt%2Fuploads%2FDWiEV7Fj87EJjqSqVDxp%2Fimage.png?alt=media&#x26;token=a682fb5b-5055-4aa9-b84b-c082c85235f0" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
To support iOS Rich Notifications, you need to add `Notification Service Extension`
{% endhint %}

In Xcode, select `Notification Service Extension` from File > New > Target

<figure><img src="https://4151189289-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSJ35x4PFzaneH3SrvcBt%2Fuploads%2FcLAf1mjkQCHQjLs1c8UE%2Fimage.png?alt=media&#x26;token=c8e6d3d0-78a7-412a-aa08-f4858ecb0c39" alt=""><figcaption></figcaption></figure>

Enter the Product Name. In this guide, we will define it as `FlareLaneNotificationServiceExtension`.

<figure><img src="https://4151189289-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSJ35x4PFzaneH3SrvcBt%2Fuploads%2FwhwUElrkgModOrdSlGMQ%2Fimage.png?alt=media&#x26;token=2053c324-a57f-4274-9b3e-e4391b456cc4" alt=""><figcaption></figcaption></figure>

Set `Minimum Deployments` of the Notification Service Extension Target you just created to be the same as the main app target you are currently using.

<figure><img src="https://4151189289-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSJ35x4PFzaneH3SrvcBt%2Fuploads%2FyrEmrmsXqdEq3PPCKPXs%2FScreenshot%202024-01-30%20at%2010.54.16%E2%80%AFAM.png?alt=media&#x26;token=47712790-1b59-4229-b2b0-4d7f391a772a" alt=""><figcaption></figcaption></figure>

***

## 4. Install SDK using CocoaPods

Add the following code to your `Podfile`

{% tabs %}
{% tab title="Podfile" %}

<pre><code>// Enable the Dynamic Framework
<strong>use_frameworks!
</strong>
target 'YOUR_PROJECT_NAME' do
  // Add the code below
<strong>  pod 'FlareLane', '1.7.4'  
</strong>end

// Add the name of the extension created previously.
// In this guide, FlareLaneNotificationServiceExtension.
<strong>target 'FlareLaneNotificationServiceExtension' do
</strong><strong>  pod 'FlareLane', '1.7.4'
</strong><strong>end
</strong></code></pre>

{% endtab %}
{% endtabs %}

Run `pod install` in the terminal

***

## 5. Initialization

### (5-1. SwiftUI Only)

Since AppDelegate.swift is not created in the SwiftUI project, the file needs to be created first.

Create a new AppDelegate.swift file and add some code to \<YOUR\_PROJECT\_NAME>App.swift file.

{% tabs %}
{% tab title="\<YOUR\_PROJECT\_NAME>App.swift" %}

<pre class="language-swift"><code class="lang-swift">struct &#x3C;YOUR_PROJECT_NAME>App: App {
    // Add the code below
<strong>    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
</strong>}
</code></pre>

{% endtab %}
{% endtabs %}

### 5-2. AppDelegate

{% tabs %}
{% tab title="Switft(AppDelegate.swift)" %}

<pre class="language-swift"><code class="lang-swift">import FlareLane

class AppDelegate: UIResponder, UIApplicationDelegate {
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {    

    // Add the code below
    // To subscribe later, set the 3rd parameter to false and then run .subscribe() later.
<strong>    FlareLane.initWithLaunchOptions(launchOptions, projectId: "&#x3C;INPUT_YOUR_PROJECT_ID>", requestPermissionOnLaunch: true)
</strong>    
    return true
  }
}
</code></pre>

{% endtab %}

{% tab title="Objective-C(AppDelegate.m)" %}

<pre class="language-objectivec"><code class="lang-objectivec">@import FlareLane

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

     // Add the code below
     // To subscribe later, set the 3rd parameter to false and then run the .subscribe() later.
<strong>     [FlareLane initWithLaunchOptions:launchOptions projectId:@"INPUT_YOUR_PROJECT_ID", requestPermissionOnLaunch:true];
</strong>}
</code></pre>

{% endtab %}
{% endtabs %}

### 5-3. Notification Service Extension

Run Xcode again and edit the extension's file. Delete the pre-filled codes and only inherit `FlareLaneNotificationServiceExtension`.

{% tabs %}
{% tab title="Swift(NorificationService.swift)" %}

<pre class="language-swift"><code class="lang-swift">
import FlareLane

<strong>class NotificationService: FlareLaneNotificationServiceExtension {
</strong>}
</code></pre>

{% endtab %}

{% tab title="Objective-C(NotificationService.h/.m)" %}

```objectivec

// NotificationService.h
@import FlareLane;

@interface NotificationService : FLNNotificationServiceExtension
@end

// NotificationService.m
#import "NotificationService.h"

@implementation NotificationService
@end
```

{% endtab %}
{% endtabs %}

***

## 6. Test

After completing all the steps, build and run your app. Devices that subscribe to push notifications will be automatically added to FlareLane console's **All Devices**.

Before actual deployment, be sure to verify that the push notifications are being received properly on the device and that the statistics are accurately recorded when the push notifications are clicked.

***

## 7. Identify your user

FlareLane's device is 'anonymous' and you can match the device with your user based on the unique user ID that identifies your user.

{% hint style="info" %}
Setting up [User ID](https://guide.flarelane.com/data-integration/user-id) has many benefits. You can distinguish whether a user has signed-up, and push notifications can be sent based on the User ID.
{% endhint %}

Usually, when a user sign-up or log-in, you should execute `setUserId`.

{% tabs %}
{% tab title="Swift" %}

```swift
FlareLane.setUserId(userId: "USER_ID")
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
[FlareLane setUserIdWithUserId: @"USER_ID"];
```

{% endtab %}
{% endtabs %}

***

## 8. Additional Setup

To use [Journeys](https://guide.flarelane.com/guide-for-developers/mobile-sdk-setup/broken-reference) or [Message Personalization](https://guide.flarelane.com/guide-for-developers/mobile-sdk-setup/broken-reference), integrate custom data via SDK or API:

* [data-integration](https://guide.flarelane.com/data-integration "mention")

***

## Resources

[mobile-sdk-reference](https://guide.flarelane.com/guide-for-developers/mobile-sdk-reference "mention")
