# 原生广告

原生广告是通过平台原本就有的界面组件向用户呈现的广告素材资源。这种广告采用您已经在 Xib 中使用的那些类进行展示，能以和应用视觉设计相称的形式呈现，让用户有浑然一体的使用体验。加载原生广告时，您的应用会收到一个包含其素材资源的广告对象，然后由应用（而不是 SDK）负责展示它们。这与其他广告格式不同，采用其他广告格式时您无法自行调整广告的外观。

## 前提条件

* 通读[入门指南](/copy-of-cn/jie-kou-wen-dang/ios/kai-shi/ru-men-zhi-nan.md)

## 务必使用测试广告进行测试

在构建和测试应用时，请确保使用的是测试广告，而不是实际投放的广告。否则，可能会导致您的账号被暂停使用。对于 iOS 原生广告，使用下面的测试专用广告单元 ID：

```
63a5739a75da4
```

该测试广告单元 ID 已经过专门配置，可确保每个请求返回的都是测试广告。您可以在自己应用的编码、测试和调试过程中随意使用该测试广告单元 ID。需要注意的一点是，请务必在发布应用前用您的广告单元 ID 替换该测试广告单元 ID。

## 接入步骤

* 请求广告
* 创建 Adx3NativeAdView
* 注册代理
* 展示广告

### 请求广告

原生广告通过 Adx3NativeAd 对象加载，由 Adx3NativeAdViewDelegate 发送相关消息。代码示例：

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

```swift
import UIKit

class ViewController: UIViewController {
    
    let nativeAd = Adx3NativeAd()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    func loadNativeAd() {
        nativeAd.load(withPlacementID: "Your Placement ID", userId: "", delegate: self)
    }

}
```

{% endtab %}

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

```objectivec
#import "Adx3Framework/Adx3Ad.h"

@interface ViewController ()

@property (strong, nonatomic) Adx3NativeAd *nativeAd;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadNativeAd];
}

- (void)loadNativeAd {
    self.nativeAd = [[Adx3NativeAd alloc] init];
    [self.nativeAd loadNativeAdWithPlacementID:@"Your Placement ID" userId:@"" delegate:self];
} 

@end
```

{% endtab %}
{% endtabs %}

### 创建 Adx3NativeAdView

创建 Adx3NativeAdView 之前，你需要创建一个继承自 Adx3NativeAdView 的 Xib 文件，实现您的自定义布局。代码示例：

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

```swift
func addNativeAdView() {
    nativeAdView = Bundle.main.loadNibNamed("Adx3NativeView", owner: nil, options: nil)?.first as? Adx3NativeAdView
    nativeAdView.translatesAutoresizingMaskIntoConstraints = false
    backgroundView.addSubview(nativeAdView)
    backgroundView.addConstraint(NSLayoutConstraint.init(item: nativeAdView!, attribute: .left, relatedBy: .equal, toItem: backgroundView, attribute: .left, multiplier: 1.0, constant: 0))
    backgroundView.addConstraint(NSLayoutConstraint.init(item: nativeAdView!, attribute: .right, relatedBy: .equal, toItem: backgroundView, attribute: .right, multiplier: 1.0, constant: 0))
    backgroundView.addConstraint(NSLayoutConstraint.init(item: nativeAdView!, attribute: .top, relatedBy: .equal, toItem: backgroundView, attribute: .top, multiplier: 1.0, constant: 0))
    backgroundView.addConstraint(NSLayoutConstraint.init(item: nativeAdView!, attribute: .bottom, relatedBy: .equal, toItem: backgroundView, attribute: .bottom, multiplier: 1.0, constant: 0))
}
```

{% endtab %}

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

```objectivec
- (void)addNativeAdView {
    [self.nativeAdView removeFromSuperview];
    self.nativeAdView = [[NSBundle mainBundle] loadNibNamed:@"Your Xib File Name" owner:nil options:nil]
        .firstObject;
    [self.nativeAdView setTranslatesAutoresizingMaskIntoConstraints:NO];

    NSDictionary *viewDictionary = NSDictionaryOfVariableBindings(_nativeAdView);
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_nativeAdVi                                            options:0
                                                                      metrics:nil
                                                                        views:viewDictionary]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_nativeAdView]|"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:viewDictionary]];
} 
```

{% endtab %}
{% endtabs %}

### 注册代理

您可以实现 Adx3NativeAdViewDelegate 方法，获取 Adx3NativeAd 对象中的广告内容，代码示例：

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

```swift
extension ViewController {    
    func nativeAdDidReceive(_ nativeAd: Adx3NativeAd) {
        nativeAdView.nativeAd = nativeAd;
        nativeAdView.titleLabel!.text = nativeAd.title;
        nativeAdView.subLabel!.text = nativeAd.shop_name;
        nativeAdView.mediaView!.urlString = nativeAd.material;
        nativeAdView.descLabel!.text = nativeAd.describe;
        
        let queue = DispatchQueue.global()
        queue.async { [self] in
            let data = try! Data.init(contentsOf: URL(string: nativeAd.logo)!)
            let image = UIImage(data: data)
            nativeAdView.iconImageView!.image = image
        }
    }
    
    func nativeAdDidClick(withPlacementID placementID: String, extra: [AnyHashable : Any]) {
        
    }
    
    func nativeAdDidFailToReceiveAdWithError(_ error: Error) {
        
    }
}

```

{% endtab %}

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

```objectivec
- (void)nativeAdDidFailToReceiveAdWithError:(NSError *)error {
    NSLog(@"----%@----",error);
}

- (void)nativeAdDidReceiveNativeAd:(Adx3NativeAd *)nativeAd {
    NSLog(@"----Native AD Load Success----");
    self.nativeAdView.nativeAd = nativeAd;
    self.nativeAdView.titleLabel.text = nativeAd.title;
    self.nativeAdView.subLabel.text = nativeAd.shop_name;
    self.nativeAdView.mediaView.urlString = nativeAd.material;
    self.nativeAdView.descLabel.text = nativeAd.describe;
    
    dispatch_queue_t queue = dispatch_queue_create(0, 0);
    dispatch_async(queue, ^{
        NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:nativeAd.logo]];
        UIImage *img=[UIImage imageWithData:data];
        dispatch_async(dispatch_get_main_queue(), ^{
            self.nativeAdView.iconImageView.image = img;
        });
    });
}

- (void)nativeAdDidClickWithPlacementID:(NSString  *)placementID extra:(NSDictionary *)extra {
    NSLog(@"----")
}
```

{% 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://docs.adx3.io/copy-of-cn/jie-kou-wen-dang/ios/guang-gao-ge-shi/yuan-sheng-guang-gao.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.
