# 激励广告

所谓激励广告，就是用户与之进行互动从而获取应用内奖励的一种广告。本指南将介绍如何将 Adx3 激励广告接入到 iOS 应用中

## 前提条件

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

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

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

```
63a573b533f6f
```

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

## 接入步骤

* 请求广告
* 注册回调
* 展示广告处理奖励事件

### 请求广告

请求激励广告通过 <mark style="color:blue;">\[Adx3RewardAd sharedManager]</mark> 中 loadAdWithPlacementID: 方法完成，loadAdWithPlacementID: 方法需要您的广告单元 ID，userId 可不传或者传入您的应用为用户设置的 userId，代码示例：

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

```swift
import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        loadRewardAd()
        // Do any additional setup after loading the view.
    }
    
    func loadRewardAd() {
        Adx3RewardAd.sharedManager().load(withPlacementID: "Your Placement ID", userId: "", delegate: self)
    }
}
```

{% endtab %}

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

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

@interface ViewController ()

@end

@implementation ViewController

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

- (void)loadRewardAd {
    [[Adx3RewardAd sharedManager] loadAdWithPlacementID:@"Your Placement ID" userId:@"" delegate:self];
}

@end
```

{% endtab %}
{% endtabs %}

### 注册回调

您可以通过实现 Adx3RewardDelegate 中的相关方法（全部可选），监听请求结果和处理广告的相关事件。代码示例：

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

```swift
import UIKit

class ViewController: UIViewController, Adx3RewardDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
}

extension ViewController {
    
    func didFinishLoadingAD(withPlacementID placementID: String!) {
        print("----Interstitial AD Load Finish----")
    }
    
    func didFailToLoadAD(withPlacementID placementID: String!, error: Error!) {
        print("----\(error.debugDescription)----")
    }
    
    func rewardDidShow(forPlacementID placementID: String!, extra: [AnyHashable : Any]!) {
        
    }
    
    func rewardDidClick(forPlacementID placementID: String!, extra: [AnyHashable : Any]!) {
        
    }
    
    func rewardDidClose(forPlacementID placementID: String!, extra: [AnyHashable : Any]!) {
        
    }
    
    func rewardDidEndPlayingVideo(forPlacementID placementID: String!, extra: [AnyHashable : Any]!) {
    
    }
    
    func rewardDidStartPlayingVideo(forPlacementID placementID: String!, extra: [AnyHashable : Any]!) {
        
    }
    
    func rewardFailedToShow(forPlacementID placementID: String!, error: Error!, extra: [AnyHashable : Any]!) {
        
    }
    
    func rewardDidFailToPlayVideo(forPlacementID placementID: String!, error: Error!, extra: [AnyHashable : Any]!) {
        
    }
    
}
```

{% endtab %}

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

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

@interface ViewController () <Adx3RewardDelegate>

@end

@implementation ViewController

-(void)didFinishLoadingADWithPlacementID:(NSString *)placementID {
    NSLog(@"----Reward AD Load Finish----");
}

-(void)didFailToLoadADWithPlacementID:(NSString*)placementID error:(NSError*)error {
    NSLog(@"----%@----",error.description);
}

-(void)rewardDidShowForPlacementID:(NSString*)placementID extra:(NSDictionary*)extra {

}

-(void)rewardFailedToShowForPlacementID:(NSString*)placementID error:(NSError*)error extra:(NSDictionary*)extra {

}

-(void)rewardDidStartPlayingVideoForPlacementID:(NSString*)placementID extra:(NSDictionary*)extra {

}

-(void)rewardDidEndPlayingVideoForPlacementID:(NSString*)placementID extra:(NSDictionary*)extra {

}

-(void)rewardDidFailToPlayVideoForPlacementID:(NSString*)placementID error:(NSError*)error extra:(NSDictionary*)extra {

}

-(void)rewardDidCloseForPlacementID:(NSString*)placementID extra:(NSDictionary*)extra {

}

-(void)rewardDidClickForPlacementID:(NSString*)placementID extra:(NSDictionary*)extra {

}

@end
```

{% endtab %}
{% endtabs %}

### 展示广告

在向用户展示激励广告之前，您必须为用户提供明确的选项，让用户可以自行选择是否通过观看激励广告来换取奖励。激励广告始终必须是用户可以选择的一种体验。代码示例：

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

```swift
func showRewardAd() {
    if Adx3RewardAd.sharedManager().rewardIsReady() {
        Adx3RewardAd.sharedManager().showReward(in: self, delegate: self)
    }
}
```

{% endtab %}

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

```objectivec
- (void)showRewardAd {
     if ([[Adx3RewardAd sharedManager] rewardIsReady]) {
        [[Adx3RewardAd sharedManager] showRewardInViewController:self delegate:self];
    }
}
```

{% 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/ji-li-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.
