原生廣告

原生廣告

以下說明適用於 v4.0.0 以後的版本。

開始使用

getData 說明

使用 getData 後所取得的 json 格式如下:

{
    "headline": "",
    "desc": "",
    "company": "",
    "image": "",
    "icon": ""
}
建立原生廣告範例

ViewController.h

#import "AdbertADNative.h"

@interface ViewController : UIViewController <AdbertADNativeDelegate>
@property(nonatomic, strong) AdbertADNative *nativeAD;

ViewController.m

//請輸入與 ADBERT 申請的 APPID 與 APPKEY
NSString *appID = @"";
NSString *appKey = @"";
self.nativeAD = [[AdbertADNative alloc] initWithAPPID:appID AndAPPKEY:appKey];

//設定 nativeAD 大小及位置
self.nativeAD.frame = CGRectMake(50, 180, 300, 100);
self.nativeAD.delegate = self;
self.nativeAD.parentView = self.view;
[self.view addSubview: self.nativeAD];

//請求廣告,初始化廣告
[self.nativeAD requestAD];
取得廣告內容與設定範例

ViewController.m

- (void)adbertADNativeDidReceiceAd:(AdbertADNative *)nativeAD {
    NSLog(@"原生廣告接收成功");
    [self showAD];
}

- (void)adbertADNative:(AdbertADNative *)nativeAD didFailToReceiveAdWithError:(NSError *)error {
    NSLog(@"原生廣告接收失敗");
    NSLog(@"%@", error);
}

- (void)showAD {
    NSDictionary *ad = self.nativeAD.getADData;
    self.nativeAD.backgroundColor = [UIColor darkGrayColor];

    //取得 Json
    NSString *companyNameString = ad[@"company"];
    NSString *headlineString = ad[@"headline"];
    NSString *imageString = ad[@"image"];

    //將物件放入 NativeADView 內
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageString]];
    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageWithData:imageData]];
    image.frame = CGRectMake(0, 0, 150, 100);
    [self.nativeAD addSubview:image];

    UILabel *companyNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.nativeAD.frame.size.height - 20, self.nativeAD.frame.size.width, 20)];
    companyNameLabel.text = companyNameString;
    [self.nativeAD addSubview:companyNameLabel];

    UILabel *headlineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.nativeAD.frame.size.width, 20)];
    headlineLabel.text = headlineString;
    [self.nativeAD addSubview:headlineLabel];

    //Show 廣告,由 SDK 加上點擊事件
    [self.nativeAD showAD];
}