# Native Ad

## Add Files for Native Ad Integration

Add the following files to your app's build target:

* `CENativeAd.h`
* `CENativeAd.m`
* `CEMediaView.h`
* `CEMediaView.m`

## Request Native Ad Instantly

With `loadAdInstant`, our synchronous method, You will get the result of loading ad directly.

* Import `CENativeAd.h` and `CEMediaView.h` Create properties for both `CENativeAd` and `CEMediaView`.

```objectivec
// MyViewController.m

#import "CENativeAd.h"
#import "CEMediaView.h"

@interface MyViewController()
```

* Create `CENativeAd` and `CEMediaView` instances. Load ad with your `CENativeAd` instance.

```objectivec
@interface MyViewController()
@property (nonatomic, strong) CENativeAd *nativeAd;
@property (nonatomic, strong) CEMediaView *mediaView;
@end

- (void) viewDidLoad() {

    self.nativeAd = [[CENativeAd alloc] init];
    [self.nativeAd setDelegate:self];
    CERequestInfo *info = [CERequestInfo new];
    info.placement = @"PUT_YOUR_PLACEMENT_STRING_HERE";
    NSError *error;
    if ([nativeAd loadAdInstantWithInfo:info error:&error]) {
        // TODO: The logic of render ad.
        self.mediaView = [[CEMediaView alloc] init];
        [self.mediaView setNativeAd:nativeAd];
    } else {
        if (error) {
            // TODO: The logic of load ad fail.
            NSString * log = [NSString stringWithFormat:@"didFailWithError : %@", error.debugDescription];
            [self appendLog:log];
        }
    }
}
```

## Request Native Ad Asynchronously

* Import `CENativeAd.h`, `CEMediaView.h` and adopt `CENativeAdRequestDelegate` protocol in view controller's extension. Create properties for both `CENativeAd` and `CEMediaView`.

```objectivec
// MyViewController.m

#import "CENativeAd.h"
#import "CEMediaView.h"

@interface MyViewController() <CENativeAdRequestDelegate>
```

* Create `CENativeAd` and `CEMediaView` instances. Load ad with your `CENativeAd` instance.

```objectivec
@interface MyViewController() <CENativeAdRequestDelegate>
@property (nonatomic, strong) CENativeAd *nativeAd;
@property (nonatomic, strong) CEMediaView *mediaView;
@end

- (void) viewDidLoad() {

    self.nativeAd = [[CENativeAd alloc] init];
    [self.nativeAd setDelegate:self];
    CERequestInfo *info = [CERequestInfo new];
    info.placement = @"PUT_YOUR_PLACEMENT_STRING_HERE";
    info.timeout = 5;
    [self.nativeAd loadAdAsyncWithInfo:info reqDelegate:self];
}
```

* Implement `CENativeAdRequestDelegate` to handle ad loaded event.

```objectivec
#pragma mark - CENativeAdRequestDelegate
- (void)nativeAdDidLoad:(CENativeAd *)nativeAd
{
    self.mediaView = [[CEMediaView alloc] init];
    //  Executed when ad is ready.
    [self.mediaView setNativeAd:nativeAd]; // Add mediaView into view hierarchy.
}
```

## Add `CENativeAdEventDelegate` to get the ad event callback.

If you want to trace the click, impression, progress event, please implement CENativeAdEventDelegate to handle it.

```objectivec
#pragma mark - CENativeADEventDelegate
@protocol CENativeAdEventDelegate <NSObject>

@optional
/*!
 *  @brief callback while native ad is about to log impression
 *
 *  @param nativeAd CENativeAd instance
 */
- (void) nativeAdWillTrackImpression:(nonnull CENativeAd *)nativeAd;

/*!
 *  @brief callback while this native ad is clicked by user
 *
 *  @param nativeAd CENativeAd instance that own this native ad component
 */
- (void) nativeAdDidClick:(nonnull CENativeAd *)nativeAd;

/*!
 *  @brief callback while native ad finished handle click event
 *
 *  @param nativeAd CENativeAd instance that own this native ad component
 */
- (void) nativeAdDidFinishHandlingClick:(nonnull CENativeAd *)nativeAd;

/*!
 *  @brief callback while this native ad is video format and muted.
 *
 *  @param nativeAd CENativeAd instance that own this native ad component
 */
- (void) nativeAdDidMute:(nonnull CENativeAd *)nativeAd;

/*!
 *  @brief callback while this native ad is video format and unmuted.
 *
 *  @param nativeAd CENativeAd instance that own this native ad component
 */
- (void) nativeAdDidUnmute:(nonnull CENativeAd *)nativeAd;

/*!
 *  @brief callback while this native ad is video format and start playback.
 *
 *  @param nativeAd CENativeAd instance that own this native ad component
 */
- (void) nativeAdDidVideoStart:(nonnull CENativeAd *)nativeAd;

/*!
 *  @brief callback while this native ad is video format and playback to end.
 *
 *  @param nativeAd CENativeAd instance that own this native ad component
 */
- (void) nativeAdDidVideoEnd:(nonnull CENativeAd *)nativeAd;

/*!
 *  @brief callback while this native ad is video format and progress state.
 *
 *  @param nativeAd CENativeAd instance that own this native ad component
 *  @param totalDuration this video total duration time (ms)
 *  @param currentPosition this video current play back position time (ms)
 */
- (void) nativeAdDidVideoProgress:(nonnull CENativeAd *)nativeAd
                         duration:(int)totalDuration
                         position:(int)currentPosition;

/*!
 *  @brief callback while this native ad is failed to render.
 *
 *  @param nativeAd CENativeAd instance that own this native ad component
 *  @param error NSError error for rendering
 */
- (void) nativeAdOnFailedToRender:(nonnull CENativeAd *)nativeAd error:(nonnull NSError *)error;
@end
```

### Please refer to [CENativeAd](/crystalexpress-documentation-v3-x/ios-sdk/api-reference/cenativead.md) for more information in detail.


---

# 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://intowow.gitbook.io/crystalexpress-documentation-v3-x/ios-sdk/quickstart-guide/step-3.-choose-ad-format/native-ad.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.
