# 네이티브 광고

## 1. 위젯 생성

네이티브를 추가하고자 하는 위치에 아래의 위젯을 생성하여 등록합니다.

Android의 경우 AndroidView 내 아래 설정을 반드시 추가합니다.

* ViewType : ‘AdPopcornSSPNativeView’
* creationParams
  * appKey
  * placementId
  * width (optional, only Android)&#x20;
  * height (optional, only Android) : 광고 영역보다 크거나 작은 영역을 광고 영역으로 잡을 경우 정확한 정렬을 위해서 android의 height는 Container의 height와 동일한 값을 넣어주시기 바랍니다

{% code title="Dart" %}

```dart
Widget _setNativeView() {
    const String viewType = 'AdPopcornSSPNativeView';
    if (Platform.isAndroid) {
      final Map<String, dynamic> creationParams = <String, dynamic>
      {'appKey':'your_app_key', 'placementId':'your_placement_id', 'height':280};
      return Container(
        width: double.maxFinite,
        height:280,
        child: AndroidView(
          viewType: viewType,
          layoutDirection: TextDirection.ltr,
          creationParams: creationParams,
          creationParamsCodec: const StandardMessageCodec(),
        ),
      );
    }
    else if (Platform.isIOS) {
      final Map<String, dynamic> creationParams = <String, dynamic>
      {'appKey':'your_app_key', 'placementId':'your_placement_id'};
      return Container(
        width: double.maxFinite,
        height:280,
        child: UiKitView(
          viewType: viewType,
          layoutDirection: TextDirection.ltr,
          creationParams: creationParams,
          creationParamsCodec: const StandardMessageCodec(),
        ),
      );
    }
    else{
      return Container(
          width: double.maxFinite,
          height: 1
      );
    }
  }
```

{% endcode %}

## 2. MethodChannel 생성

네이티브의 이벤트를 전달 받고 싶은 경우, 아래의 규칙으로 method channel을 생성합니다.

* <mark style="color:red;">`MethodChannel`</mark>('adpopcornssp/<mark style="color:blue;">`{your_placement_id}`</mark>')

생성한 MethodChannel에 <mark style="color:red;">`methodCallHandler`</mark>를 세팅할 경우, 아래 이벤트를 전달 받을 수 있습니다.

| 이벤트                      | 설명                                                           |
| ------------------------ | ------------------------------------------------------------ |
| APSSPNativeAdLoadSuccess | 네이티브 광고 로드 성공                                                |
| APSSPNativeAdLoadFail    | 네이티브 광고 로드 실패. [에러코드 값](/ssp-sdk/sdk/android/undefined-9.md) |
| APSSPNativeAdImpression  | 네이티브 광고 노출 성공                                                |
| APSSPNativeAdClicked     | 네이티브 광고 클릭                                                   |

{% code title="Dart" %}

```dart
class _MyAppState extends State<MyApp> {

static const MethodChannel androidNativeChannel = const MethodChannel('adpopcornssp/your_placement_id');
  static const MethodChannel iosNativeChannel = const MethodChannel('adpopcornssp/your_placement_id');


 @override
  void initState() {
	  super.initState();
    if (Platform.isAndroid) {
      AdPopcornSSP.init('663451319');

      // 네이티브 이벤트 채널 연동
      androidBannerChannel.setMethodCallHandler(_eventHandleMethod);
    } else if (Platform.isIOS) {
      AdPopcornSSP.init('397261446');

      // 네이티브 이벤트 채널 연동
      iosNativeChannel.setMethodCallHandler(_eventHandleMethod);
    }
  }
  
  static Future<dynamic> _eventHandleMethod(MethodCall call) {
      final Map<dynamic, dynamic> arguments = call.arguments;
      final String method = call.method;

      final String placementId = arguments['placementId'];
      if (method == 'APSSPNativeAdLoadSuccess') {
      } else if (method == 'APSSPNativeAdLoadFail') {
        final int errorCode = arguments['errorCode'];
      } else if (method == 'APSSPNativeAdImpression') {
      } else if (method == 'APSSPNativeAdClicked') {
      }
      return Future<dynamic>.value(null);
    }
}
```

{% endcode %}


---

# 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://adpopcornssp.gitbook.io/ssp-sdk/sdk/flutter/undefined-6.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.
