# 전면 광고

### 1 : 준비

* [프로젝트 설정](/adpie/ios/project-settings.md)을 통해 광고 연동을 위한 준비를 합니다.

### 2 : SDK 초기화

* 최초 SDK 를 초기화 완료 후, 광고 요청이 가능합니다.
* [사이트](http://my.adpies.com)에서 발급받은 `Media ID`를 입력합니다.

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

```swift
import UIKit
import AdPieSDK
import AppTrackingTransparency

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application(_ application: UIApplication, 
        didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 
    {
        return true
    }

    func applicationDidBecomeActive(_ application: UIApplication) {

        func initializeSDK() {
            AdPieSDK.sharedInstance().initWithMediaId("YOUR_MEDIA_ID")
        }
    
        if #available(iOS 14.5, *) {
            ATTrackingManager.requestTrackingAuthorization { _ in
                initializeSDK()
            }
        } else {
            initializeSDK()
        }
    }
}
```

{% endtab %}

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

```objectivec
// AppDelegate.m
#import "AppDelegate.h"
#import <AdPieSDK/AdPieSDK.h>
#import <AppTrackingTransparency/AppTrackingTransparency.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application 
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
    return YES;
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    if (@available(iOS 14.5, *)) {
        [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(_){
            [self initializeSDK];
        }];
    } else {
        [self initializeSDK];
    }
}

- (void)initializeSDK {
    [[AdPieSDK sharedInstance] initWithMediaId:@"YOUR_MEDIA_ID_HERE"];
}

@end
```

{% endtab %}
{% endtabs %}

### 3 : 광고 요청

* ViewController에 광고 요청을 위한 객체를 생성합니다.
* [사이트](http://my.adpies.com)에서 발급받은 Slot ID를 입력합니다.
* 광고 요청과 표출로 메소드가 각각 나누어져 있기에, 요청을 미리하고 표출을 필요할 때 합니다.(Preload)

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

```swift
import UIKit
import AdPieSDK

class ViewController: UIViewController, APInterstitialDelegate {

    var interstitial: APInterstitial!
    
    override func viewDidLoad() { 
        super.viewDidLoad()
        // 광고 객체 생성 (Slot ID 입력)
        interstitial = APInterstitial(slotId: "YOUR_SLOT_ID_HERE")
        // 델리게이트 등록
        interstitial.delegate = self
        // 광고 요청
        interstitial.load()
    }
}
```

{% endtab %}

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

```objectivec
// ViewController.h
#import <AdPieSDK/AdPieSDK.h>

@interface ViewController : UIViewController <APInterstitialDelegate>
@property APInterstitial *interstitial;
@end

// ViewController.m
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 광고 객체 생성 (Slot ID 입력)
    self.interstitial = 
        [[APInterstitial alloc] initWithSlotId:@"YOUR_SLOT_ID_HERE"];
    // 델리게이트 등록
    self.interstitial.delegate = self;
    // 광고 요청
    [self.interstitial load];
}

@end
```

{% endtab %}
{% endtabs %}

### 4 : 광고 표출 (광고 이벤트 수신)

* 광고 호출에 대한 콜백을 받을 수 있습니다.
* ViewController에서 `APInterstitialDelegate` 프로토콜을 도입(adopt)합니다.
* 광고 객체의 delegate 프로퍼티에 콜백(이벤트)를 수신받을 객체를 설정합니다.
* 델리게이트의 메소드 중 성공, 실패에 대한 메소드는 필수사항으로 반드시 구현해야 하며, 나머지 메소드는 필요에 따라서 구현합니다.
* 광고를 요청하기 위해서는 `load`를 호출하고, 표출하기 위해서는 swift에서는 `present` 메소드를 호출하고, Objective-C 언어에서는  `presentFromRootViewController:` 메소드를 호출합니다.
* [에러코드](/adpie/ios/common/errorcode.md)를 통해 광고 실패에 대한 이유를 알 수 있습니다.

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

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

class ViewController: UIViewController, APInterstitialDelegate {

    ...
        
    // MARK: - APInterstitial delegates    
    func interstitialDidLoadAd(_ interstitial: APInterstitial!) {
        // 광고 로딩 완료 후 이벤트 발생 (성공)
        
        // 광고 요청 후 즉시 노출하고자 할 경우 아래의 코드를 추가합니다.
        if interstitial.isReady() {
            // 광고 표출
            interstitial.present(fromRootViewController: self)
        }
    }
    
    func interstitialDidFail(toLoadAd interstitial: APInterstitial!, withError error: Error!) {
        // 광고 요청 실패 후 이벤트 발생
        // error code : error._code
        // error message : error.localizedDescription
    }
    
    func interstitialDidFail(toShowAd interstitial: APInterstitial!, withError error: (any Error)!) {
        // 광고 표출 실패
    }
    
    func interstitialWillPresentScreen(_ interstitial: APInterstitial!) {
        // 광고 표출 후 이벤트 발생
    }
    
    func interstitialWillDismissScreen(_ interstitial: APInterstitial!) {
        // 광고가 표출한 뒤 종료하기 전에 이벤트 발생
    }
    
    func interstitialDidDismissScreen(_ interstitial: APInterstitial!) {
        // 광고가 표출한 뒤 종료한 후 이벤트 발생
    }
    
    func interstitialWillLeaveApplication(_ interstitial: APInterstitial!) {
        // 광고 클릭 후 이벤트 발생
    }
    
<strong>    func videoFinished(_ interstitial: APInterstitial!, videoFinishState: APVideoFinishState) {
</strong>        // 동영상 광고 종료 알림 (iOS AdPie SDK 1.6.10 지원)
        switch videoFinishState {
        case .error:
            print("동영상 광고 오류")
        case .skipped:
            print("동영상 광고 건너뜀")
        case .completed:
            print("동영상 광고 시청완료")
        default:
            break
    }
}
</code></pre>

{% endtab %}

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

```objectivec
// ViewController.m
@implementation ViewController

#pragma mark APInterstitial delegates

- (void)interstitialDidLoadAd:(APInterstitial *)interstitial {
    // 광고 로딩 완료 후 이벤트 발생 (성공)
    // 광고 요청 후 즉시 노출하고자 할 경우 아래의 코드를 추가합니다.
    if ([self.interstitial isReady]) {
        // 광고 표출
        [self.interstitial presentFromRootViewController:self];
    }
}

- (void)interstitialDidFailToLoadAd:(APInterstitial *)interstitial 
    withError:(NSError *)error 
{
    // 광고 요청 실패 후 이벤트 발생
    // error code : [error code]
    // error message : [error localizedDescription]
}

- (void)interstitialDidFailToShowAd:(APInterstitial *)interstitial 
   withError:(NSError *)error 
{
    // 광고 표출 실패
}

- (void)interstitialWillPresentScreen:(APInterstitial *)interstitial {
    // 광고 표출 후 이벤트 발생
}

- (void)interstitialWillDismissScreen:(APInterstitial *)interstitial {
    // 광고가 표출한 뒤 종료하기 전에 이벤트 발생
}

- (void)interstitialDidDismissScreen:(APInterstitial *)interstitial {
    // 광고가 표출한 뒤 종료한 후 이벤트 발생
}

- (void)interstitialWillLeaveApplication:(APInterstitial *)interstitial {
    // 광고 클릭 후 이벤트 발생
}

- (void)videoFinished:(APInterstitial *)interstitial videoFinishState:(APVideoFinishState)finishState {
    // 동영상 광고 종료 알림 (iOS AdPie SDK 1.6.10 지원)
    switch (videoFinishState) {
        case APVideoFinishStateError:       break;  // 동영상 광고 오류
        case APVideoFinishStateSkipped:     break;  // 동영상 광고 건너띔
        case APVideoFinishStateCompleted:   break;  // 동영상 광고 시청완료
        default:
            break;
    }
}

@end
```

{% endtab %}
{% endtabs %}


---

# 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://platform-business.gitbook.io/adpie/ios/integration/interstitial.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.
