# 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](https://intowow.gitbook.io/crystalexpress-documentation-v3-x/ios-sdk/api-reference/cenativead) for more information in detail.
