原生广告
原生广告是通过平台原本就有的界面组件向用户呈现的广告素材资源。这种广告采用您已经在 Xib 中使用的那些类进行展示,能以和应用视觉设计相称的形式呈现,让用户有浑然一体的使用体验。加载原生广告时,您的应用会收到一个包含其素材资源的广告对象,然后由应用(而不是 SDK)负责展示它们。这与其他广告格式不同,采用其他广告格式时您无法自行调整广告的外观。
前提条件
通读入门指南
务必使用测试广告进行测试
在构建和测试应用时,请确保使用的是测试广告,而不是实际投放的广告。否则,可能会导致您的账号被暂停使用。对于 iOS 原生广告,使用下面的测试专用广告单元 ID:
63a5739a75da4
该测试广告单元 ID 已经过专门配置,可确保每个请求返回的都是测试广告。您可以在自己应用的编码、测试和调试过程中随意使用该测试广告单元 ID。需要注意的一点是,请务必在发布应用前用您的广告单元 ID 替换该测试广告单元 ID。
接入步骤
请求广告
创建 Adx3NativeAdView
注册代理
展示广告
请求广告
原生广告通过 Adx3NativeAd 对象加载,由 Adx3NativeAdViewDelegate 发送相关消息。代码示例:
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)
}
}
#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
创建 Adx3NativeAdView
创建 Adx3NativeAdView 之前,你需要创建一个继承自 Adx3NativeAdView 的 Xib 文件,实现您的自定义布局。代码示例:
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))
}
- (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]];
}
注册代理
您可以实现 Adx3NativeAdViewDelegate 方法,获取 Adx3NativeAd 对象中的广告内容,代码示例:
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) {
}
}
- (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(@"----")
}
展示广告
请求和创建广告后,剩下的工作就是将广告展示给用户了。
Last updated