IOS Anchor

出自TAMedia
於 2018年6月5日 (二) 05:44 由 imported>Wikiuser 所做的修訂
跳至導覽 跳至搜尋

Anchor廣告的特性是會位於ViewController正下方,圖片依照640x280、影片依照560x315的比例,在不超過畫面高度1/3內,自動fit螢幕寬度,當螢幕轉向時因高度改變會依照上述特性調整廣告尺寸。

回 iOS首頁

加入 TADInReadAnchor

  • 匯入廣告標頭檔
  • 在 UIViewController 中宣告 TADInReadAdAnchor 執行個體
  • 帶入需要呈現廣告的ViewController,初始化廣告
  • 建立Request請求廣告
@interface AnchorSampleVC () <TADInReadAnchorDelegate>
@property (nonatomic,strong) TADInReadAnchor *inReadAnchorAd;
@end

@implementation AnchorSampleVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestAnchor];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
     // 通知廣告頁面將出現
    [self.inReadAnchorAd viewWillAppear];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    // 通知廣告頁面將消失
    [self.inReadAnchorAd viewWillDisappear];
}

- (void)requestAnchor {
    //帶入需要呈現廣告的ViewController
    self.inReadAnchorAd = [[TADInReadAnchor alloc] initAnchorWithViewController:self];

    // 設定 AD Unit ID
    self.inReadAnchorAd.adUnitID = [_YOUR_AD_UNIT_ID_];
 
    // 必須要設定delegate
    self.inReadAnchorAd.delegate = self;

    TADRequest *request = [TADRequest request];
    // 顯示log     
    request.showLog = YES;
    
    // Type: TADGender
    request.gender = [_GENDER_];
    
    // Set birthday
    [request setBirthdayWithYear:[_YEAR_] month:[_MONTH_] day:[_DAY_]];
    
    // Set location
    [request setLocationWithLatitude:[_LATITUDE_] longitude:[_LONGITUDE_] accuracy:[_ACCURACY_]];    

    // 載入廣告
    [self.inReadAnchorAd loadRequest:request];
}

TADInReadAdRectDelegate

// 廣告載入成功後被呼叫
- (void)anchorDidReceiveAd:(TADInReadAnchor *)anchor {
}

// 廣告載入失敗後被呼叫
- (void)anchor:(TADInReadAnchor *)anchor didFailToReceiveAdWithError:(TADRequestError *)error {
}
 // 廣告將出現時呼叫
- (void)anchorWillShow:(TADInReadAnchor *)anchor {
}
 // 應用程式及將改在背景執行或中止運作前被呼叫
- (void)anchorWillLeaveApplication:(TADInReadAnchor *)anchor {
}

//使用者將anchor關閉時呼叫
- (void)anchorUserPressedCloseAd {
    //user關閉anchor後 須將廣告及delegate設為nil
    self.inReadAnchorAd.delegate = nil;
    self.inReadAnchorAd = nil;
}

特別注意

  1. viewController消失前, 必須將 inReadAnchor 及其 delegate 設為 nil
   - (IBAction)backButtonPressed:(id)sender {
       [self dismissViewControllerAnimated:YES completion:nil];
           if (self.inReadAnchorAd != nil) {
               self.inReadAnchorAd.delegate = nil;
               self.inReadAnchorAd = nil;
           }
   }