> For the complete documentation index, see [llms.txt](https://docs.dfinery.ai/developer-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.dfinery.ai/developer-guide/platform/flutter/integration.md).

# 연동하기

이 문서는 Dfinery SDK를 Flutter에 통합하는 방법을 다룹니다. Dfinery SDK를 설치하면 이벤트 분석 기능과 유저 프로필 기능 그리고 액션 기능이 제공됩니다.

더 자세히 알아보려면 [리소스 및 샘플](/developer-guide/common/resource-and-sample.md)을 참조하세요.

## 시작하기 전에 <a href="#before-you-begin" id="before-you-begin"></a>

### 서비스 설정 <a href="#service-setup" id="service-setup"></a>

[DFINERY 콘솔](https://console.dfinery.ai/)의 서비스 관리/서비스 정보 페이지에서 데이터 소스 항목에 사용하시려는 플랫폼을 체크하셔야 합니다.

![서비스 정보](/files/kByxg9Ww4QXDmsd2ecNT)

### 지원 정보 <a href="#support-info" id="support-info"></a>

#### Android

* 최소 지원 SDK : Android 4.4+ / API 19+
* Compile SDK : 34

#### iOS

* iOS 12.0
* Xcode 16.0

### 의존성 <a href="#dependencies" id="dependencies"></a>

#### Android

* [androidx.core:core:1.16.0](https://mvnrepository.com/artifact/androidx.core/core)
* [com.google.android.gms:play-services-appset:16.0.2](https://mvnrepository.com/artifact/com.google.android.gms/play-services-appset)
* [com.google.android.gms:play-services-ads-identifier:18.0.1](https://mvnrepository.com/artifact/com.google.android.gms/play-services-ads-identifier)
* [com.google.firebase:firebase-messaging:24.0.0](https://mvnrepository.com/artifact/com.google.firebase/firebase-messaging)

## SDK 설치 <a href="#sdk-installation" id="sdk-installation"></a>

### pubspec.yaml에 Dfinery 패키지를 추가합니다. <a href="#add-package" id="add-package"></a>

Flutter 프로젝트의 `pubspec.yaml` 파일에 다음과 같이 Dfinery Flutter SDK 패키지를 추가하세요.

```yaml
dependencies:
  dfinery_plugin: ^1.0.0
```

패키지를 추가한 후 터미널에서 다음 명령어를 실행하세요:

```bash
flutter pub get
```

### SDK 설정하기 <a href="#sdk-setup" id="sdk-setup"></a>

{% tabs %}
{% tab title="Android" %}
**1. 권한 추가**

`android/app/src/main/AndroidManifest.xml`에 필요한 권한을 등록해주세요.

```xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
```

#### 2. 설정 완료.

다음은 작성이 완료된 `AndroidManifest.xml` 예시입니다.

```xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapp">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:allowBackup="false"
      android:theme="@style/AppTheme">
      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
        android:launchMode="singleTask"
        android:windowSoftInputMode="adjustResize"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
    </application>
</manifest>
```

{% endtab %}

{% tab title="iOS" %}
프로젝트의 Root 경로에서 터미널을 실행시킨 다음 입력하세요.

```bash
cd ios && pod install
```

{% endtab %}
{% endtabs %}

## SDK 초기화 <a href="#sdk-initialization" id="sdk-initialization"></a>

### 초기화 하기 <a href="#initialization" id="initialization"></a>

앱에서 Dfinery SDK를 초기화하려면 다음 단계를 완료하세요.

#### 1. main.dart에 Dfinery를 import합니다.

```dart
import 'package:dfinery_plugin/dfinery_plugin.dart';
```

#### 2. 앱 초기화 시점에 다음 코드를 작성합니다.

{% hint style="info" %}
Service ID는 [DFINERY Console](https://console.dfinery.ai/)의 `서비스 관리/서비스 정보/Key 정보/서비스키` 경로에서 확인이 가능합니다.
{% endhint %}

```dart
import 'package:dfinery_plugin/dfinery_plugin.dart';

Dfinery.init(serviceId: "YOUR_SERVICE_ID");
```

{% hint style="info" %}
SDK의 초기화는 `initState()` 혹은 그 이전에 호출된다면 `WidgetsFlutterBinding.ensureInitialized()` 직후에 호출해주세요.
{% endhint %}

#### 3. 초기화가 완료되었습니다.

### 구글 광고 ID 설정하기(선택사항) <a href="#set-google-a-d-id" id="set-google-a-d-id"></a>

Google 광고 ID를 수집하기 위해선 `setGoogleAdvertisingId()` 메소드를 통해 수동으로 설정해야합니다.

#### 1. android/app/src/main/AndroidManifest.xml에 필요한 권한을 추가합니다.

```xml
<uses-permission android:name="com.google.android.gms.permission.AD_ID" />
```

#### 2. `setGoogleAdvertisingId()` 메소드를 통해 광고 ID를 설정합니다.

{% hint style="info" %}
`Dfinery.getGoogleAdvertisingId()`은 [Play Services Ads Identifier](https://mvnrepository.com/artifact/com.google.android.gms/play-services-ads-identifier) API를 사용하여 광고 ID를 가져옵니다. 다른 방법으로 광고 ID를 가져와 설정하셔도 무관합니다.
{% endhint %}

```dart
void refreshGoogleAdvertisingId() {
  Dfinery.getGoogleAdvertisingId().then((value) {
    if (value != null) {
      Dfinery.setGoogleAdvertisingId(
          googleAdvertisingId: value.googleAdvertisingId,
          isLimitAdTrackingEnabled: value.isLimitAdTrackingEnabled);
    }
  }).catchError((error) {
    print("error in refreshGoogleAdvertisingId. ${error}");
  });
}
```

## SDK 설정 <a href="#sdk-configuration" id="sdk-configuration"></a>

SDK 초기화 시에 로그 활성화 등의 옵션을 설정할 수 있습니다.

### 로그 활성화 하기 <a href="#enable-logging" id="enable-logging"></a>

플랫폼에 따라 `DFConfig.iosLogEnable` 혹은 `DFConfig.androidLogEnable`를 사용합니다.

```dart
Map<String, dynamic> config = {
  DFConfig.iosLogEnable: true,
  DFConfig.androidLogEnable: true,
};
Dfinery.initWithConfig(serviceId: "YOUR_SERVICE_ID", config: config);
```

### 로그 레벨 변경하기 <a href="#change-log-level" id="change-log-level"></a>

`DFConfig.androidLogLevel`를 사용하여 Android 플랫폼의 로그 레벨 설정을 변경할 수 있습니다.

{% hint style="info" %}
로그 레벨 값은 [`android.util.Log`](https://developer.android.com/reference/android/util/Log#constants_1)의 상수 값의 정의를 따릅니다.
{% endhint %}

* DFAndroidLogLevel
  * verbose : `2`
  * debug : `3`
  * info : `4`
  * warn : `5`
  * error : `6`
  * assert : `7`

```dart
Map<String, dynamic> config = {
  DFConfig.androidLogLevel: DFAndroidLogLevel.verbose
};
Dfinery.initWithConfig(serviceId: "YOUR_SERVICE_ID", config: config);
```

## 완료 <a href="#complete" id="complete"></a>

SDK 설치 및 초기화가 완료되었습니다.
