IOS InReadAd

出自TAMedia
於 2018年6月8日 (五) 09:21 由 imported>Wikiuser 所做的修訂 →‎特別注意
跳至導覽 跳至搜尋

InRead廣告的特性是當廣告位置呈現在螢幕上時,會自動觸發播放廣告影片以及回報曝光Log,適合在需要滾動的畫面中加入(ex:UIScrollView,UITableView)。目前提供兩種尺寸:

  1. 300x250:固定廣告尺寸為寬300,高250
  2. 1200x627:會自動fit super view的寬度,並保持比例為1200:627,因為size是在super view addSubview時決定,故加入前請確認super view寬高不會再更動。

注意

  • iOS InRead廣告不適用於可轉動方向的App。
  • 加入InRead廣告前,需要在super view size已經確定的情況才加入,避免廣告自動計算寬高發生錯誤。
  • 加入UITableView及UICollectionView時,需在willDisplayCell的地方加入,自動播放功能才可正常播放。(參見特別注意2)


回 iOS首頁

加入 TADInReadAdRect

TADInReadRect廣告用法與其他廣告類似

  • 匯入廣告標頭檔
  • 在 UIViewController 中宣告 TADInReadAdRect 執行個體
  • 初始化廣告,InReadAd300x250或是InReadAd1200x627
  • 建立Request請求廣告
   @interface InReadAdRectSampleVC () <TADInReadAdRectDelegate>
   @property (nonatomic,strong) TADInReadAdRect *inReadRectAd;
   @end
   
   @implementation InReadAdRectSampleVC
   
   - (void)viewDidLoad {
       [super viewDidLoad];
   }
   - (void)viewWillAppear:(BOOL)animated {
       [super viewWillAppear:animated];
    // 通知廣告頁面將出現
       [self.inReadRectAd viewWillAppear];
   }
   - (void)viewDidAppear:(BOOL)animated {
       [super viewDidAppear:animated];
       [self requestAd];
   }
   - (void)viewWillDisappear:(BOOL)animated {
       [super viewWillDisappear:animated];
    // 通知廣告頁面將消失
       [self.inReadRectAd viewWillDisappear];
   }
   - (void)requestAd {
    // 初始化InReadAd,帶入所需要的InReadType(依需求選擇300x250或1200x627),以及原點位置
       self.inReadRectAd = [[TADInReadAdRect alloc] initWithInReadType:InReadAd1200x627 origin:CGPointMake(0,0)];

    // 設定 AD Unit ID
       self.inReadRectAd.adUnitID = [_YOUR_AD_UNIT_ID_];

    // 必須要設定delegate
       self.inReadRectAd.delegate = self;

    // 是否同意讓廣告控制audioSessionControl default為Yes,依需求修改
       self.inReadRectAd1200x627.allowAudioSessionControl = YES;

       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.inReadRect loadRequest:request];

    // 加入廣告
       [self.view addSubview:self.inReadRectAd];
   }

TADInReadAdRectDelegate

    // 廣告載入成功後被呼叫, 可以在這裡呈現廣告
   - (void)inReadAdRectViewDidReceiveAd:(TADInReadAdRect *)view {    
   }
   // 廣告載入失敗後被呼叫
   - (void)inReadAdRectView:(TADInReadAdRect *)view didFailToReceiveAdWithError:(TADRequestError *)error {
   }
   // 廣告將出現於螢幕    
   - (void)inReadAdRectViewPresentedOnScreen:(TADInReadAdRect *)adView {    
   }
   // 廣告將消失於螢幕
   - (void)inReadAdRectViewDismissedOnScreen:(TADInReadAdRect *)adView {
   }
    // 應用程式及將改在背景執行或中止運作前被呼叫
   - (void)inReadAdRectViewWillLeaveApplication:(TADInReadAdRect *)adView {
   
   }

特別注意

  • viewController消失前, 必須將 inReadRectAd 及其 delegate 設為 nil
   - (IBAction)backButtonPressed:(id)sender {
       [self dismissViewControllerAnimated:YES completion:nil];
           if (self.inReadRectAd != nil) {
               self.inReadRectAd.delegate = nil;
               self.inReadRectAd = nil;
           }
   }
  • 若是要加入UITableView 或 UICollectionView須在willDisplayCell的地方加入廣告
   - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
     //在TableView加入InReadRectAd需在willDisplayCell時加入
     if (indexPath.row == 15 && !self.inReadRectAd.superview) {
         [cell.contentView addSubview:self.inReadRectAd];
     }
   }
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
   //在CollectionView加入InReadRectAd需在willDisplayCell時加入
   if (indexPath.row == 15 && !self.inReadRectAd.superview) {
       [cell.contentView addSubview:self.inReadRectAd];
   }
}