Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Expand
titleObjective-C
Code Block
languageobjective-c
@interface CustomAdViewHolder: BABAdViewHolder {
  BABNativeAdView *_adView;
  BABMediaView *_mediaView;
  UILabel *_titleLabel;
  ...
}
@end

@implementation CustomAdViewHolder

- (instancetype)initWithCoder:(NSCoder *)coder {
  if (self = [super initWithCoder:coder]) {
    [self setUpView];
  }
  return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
  if (self = [super initWithFrame:frame]) {
    [self setUpView];
  }
  return self;
}

- (void)setUpView {
  _adView = [[BABNativeAdView alloc] initWithFrame:CGRectZero];
  _mediaView = [[BABMediaView alloc] initWithFrame:CGRectZero];
  _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  ...
  
  _adView.delegate = self;
  
  [self addSubview:_adView];
  [_adView addSubview:_mediaView];
  [_adView addSubview:_titleLabel];
  ...
  
  // LayoutConstraint 설정
  ...
}

// Bind view with ad
- (void)renderAd:(BABAd *)ad {
    [super renderAd:ad];
  
    self.titleLabel.text = ad.creative.title;
    self.descriptionLabel.text = ad.creative.body;
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:ad.creative.iconUrl]];
  
    self.adView.ad = ad;
    self.adView.mediaView = self.mediaView;
    self.adView.clickableViews = @[self.ctaButton, self.iconImageView];
    
    [self updateCtaButtonWithAd:ad];
}

- (void)updateCtaButtonWithAd:(BABAd *)ad {
  NSString *callToAction = ad.creative.callToAction;
  double reward = [ad.reward getAvailableReward];
  double totalReward = [ad getTotalReward];
  BOOL isParticipated = [ad isParticipated];
  BOOL isClicked = [ad isClicked];
  BOOL isActionType = [ad isActionType];
  if (isClicked && isActionType && !isParticipated) {
    _rewardIcon.image = nil;
    _ctaLabel.text = @"참여 확인 중";
  } else {
    if (totalReward > 0 && isParticipated) {
      _rewardIcon.image = [UIImage imageNamed:@"ic_check"];
      _ctaLabel.text = @"참여 완료";
    } else if (reward > 0) {
      _rewardIcon.image = [UIImage imageNamed:@"ic_coin"];
      _ctaLabel.text = [NSString stringWithFormat:@"+%d %@", (int)reward, callToAction];
    } else {
      _rewardIcon.image = nil;
      _ctaLabel.text = callToAction;
    }
  }
}

#pragma mark -- BABNativeAdViewDelegate
- (void)BABNativeAdView:(BABNativeAdView *)adView didImpressAd:(BABAd *)ad {
}

- (void)BABNativeAdView:(BABNativeAdView *)adView didClickAd:(BABAd *)ad {
  [self updateCtaButtonWithAd:ad];
}

- (void)BABNativeAdView:(BABNativeAdView *)adView willRequestRewardForAd:(BABAd *)ad {
}

- (void)BABNativeAdView:(BABNativeAdView *)adView didRewardForAd:(BABAd *)ad withResult:(BABRewardResult)result {
  [self updateCtaButtonWithAd:ad];
}

- (void)BABNativeAdView:(BABNativeAdView *)adView didParticipateAd:(BABAd *)ad {
  [self updateCtaButtonWithAd:ad];
}

@end
Code Block
languageobjective-c
BABFeedConfig *config = [[BABFeedConfig alloc] initWithUnitId:@"YOUR_FEED_UNIT_ID"];
config.adViewHolderClass = [CustomAdViewHolder class];
Expand
titleSwift
Code Block
languageswift
class CustomAdViewHolder: BABAdViewHolder {
  private var adView = BABNativeAdView()
  private var mediaView = BABMediaView()
  private var titleLabel = UILabel()
  ...

  required init?(coder: NSCoder) {
    super.init(coder: coder)
    setUpView()
  }
  
  override init(frame: CGRect) {
    super.init(frame: frame)
    setUpView()
  }
  
  func setUpView() {
    adView.delegate = self
    addSubview(self.adView)
    adView.addSubview(self.mediaView)
    adView.addSubview(self.titleLabel)
    ...
    
    // LayoutConstraint 설정
    ...
  }

  // Bind view with ad
  override func renderAd(_ ad: BABAd) {
    super.renderAd(ad)
    
    self.titleLabel.text = ad.creative.title
    self.descriptionLabel.text = ad.creative.body
    
    if let urlString = ad.creative.iconUrl {
      self.iconImageView.sd_setImage(with: URL(string: urlString))
    }
    
    self.adView.ad = ad
    self.adView.mediaView = self.mediaView
    self.adView.clickableViews = [self.ctaButton, self.iconImageView]
    
    updateCtaButton(ad: ad)
  }
  
  func updateCtaButton(ad: BABAd) {
    let callToAction = ad.creative.callToAction ?? "디폴트 스트링"
    let reward = ad.rewardgetAvailableReward()
    let totalReward = ad.getTotalReward()
    let isParticipated = ad.isParticipated()
    let isClicked = ad.isClicked()
    let isActionType = ad.isActionType()
    if (isClicked && isActionType && !isParticipated) {
      rewardIcon.image = nil
      ctaLabel.text = "참여 확인 중"
    } else {
      if (totalReward > 0 && isParticipated) {
        rewardIcon.image = UIImage(named: "ic_check")
        ctaLabel.text = "참여 완료"
      } else if (reward > 0) {
        rewardIcon.image = UIImage(named: "ic_coin")
        ctaLabel.text = "\(Int(ad.reward))P \(callToAction)"
      } else {
        rewardIcon.image = nil
        ctaLabel.text = callToAction
      }
    }
  }
  
  // MARK: BABNativeAdViewDelegate
  func babNativeAdView(_ adView: BABNativeAdView, didImpress ad: BABAd) {
  }
  
  func babNativeAdView(_ adView: BABNativeAdView, didClick ad: BABAd) {
    updateCtaButton(ad: ad)
  }
  
  func babNativeAdView(_ adView: BABNativeAdView, willRequestRewardFor ad: BABAd) {
  }
  
  func babNativeAdView(_ adView: BABNativeAdView, didRewardFor ad: BABAd, with result: BABRewardResult) {
    updateCtaButton(ad: ad)
  }
  
  func babNativeAdView(_ adView: BABNativeAdView, didParticipateAd ad: BABAd) {
    updateCtaButton(ad: ad)
  }
}
Code Block
languageswift
let config = BABFeedConfig(unitId: "YOUR_FEED_UNIT_ID")
config.adViewHolderClass = CustomAdViewHolder.self

...

Expand
titleObjective-C
Code Block
languageobjective-c
@interface CustomCpsAdViewHolder: BABAdViewHolder {
  BABNativeAdView *_adView;
  BABMediaView *_mediaView;
  UILabel *_titleLabel;
  ...
}
@end

@implementation CustomCpsAdViewHolder

- (instancetype)initWithCoder:(NSCoder *)coder {
  if (self = [super initWithCoder:coder]) {
    [self setUpView];
  }
  return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
  if (self = [super initWithFrame:frame]) {
    [self setUpView];
  }
  return self;
}

- (void)setUpView {
  _adView = [[BABNativeAdView alloc] initWithFrame:CGRectZero];
  _mediaView = [[BABMediaView alloc] initWithFrame:CGRectZero];
  _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  
  _adView.delegate = self;
  
  [self addSubview:_adView];
  [_adView addSubview:_mediaView];
  [_adView addSubview:_titleLabel];
  ...
}

// 1) Populate your view (example using xib file)
- (void)loadFromNib {
  self.adView = [[NSBundle bundleForClass:[CustomCpsAdViewHolder class]] loadNibNamed:@"CustomAdViewHolder" owner:self options:nil][0];
  [self addSubview:self.adView];
}

// 2) Bind view with ad
- (void)renderAd:(BABAd *)ad {
  [super renderAd:ad];
  
  _titleLabel.text = ad.creative.title;
  
  // description 필드가 objc 객체에 기본으로 있어서 body로 컨버팅 하여 사용
  _descriptionLabel.text = ad.creative.body;
  
  [_iconImageView sd_setImageWithURL:[NSURL URLWithString:ad.creative.iconUrl]];
  
  // for CPS
  BABProduct *product = ad.product;
  if (product.discountedPrice != 0) {
    _priceView.text = [self formattedStringForNumber:@(product.discountedPrice)];
    _originalPriceView.text = [self formattedStringForNumber:@(product.price)];
    CGFloat discountRate = (1 - product.discountedPrice / product.price) * 100;
    _discountRateView.text = [NSString stringWithFormat:@"%.f%%", discountRate];
  } else {
    _priceView.text = [self formattedStringForNumber:@(product.price)];
    _originalPriceView.text = [self formattedStringForNumber:@(product.price)];
    _discountRateView.text = @"0%";
  }
  
  if (product.category != nil) {
    _categoryView.text = ad.product.category;
  } else {
    _categoryView.hidden = YES;
  }
  
  _adView.ad = ad;
  _adView.mediaView = _mediaView;
  _adView.clickableViews = @[_ctaButton, _iconImageView];
  
  [self updateCtaButtonWithAd:ad];
}

- (void)updateCtaButtonWithAd:(BABAd *)ad {
  NSString *callToAction = ad.creative.callToAction;
  double reward = [ad.reward getAvailableReward];
  double totalReward = [ad getTotalReward];
  BOOL isParticipated = [ad isParticipated];
  BOOL isClicked = [ad isClicked];
  BOOL isActionType = [ad isActionType];
  if (isClicked && isActionType && !isParticipated) {
    _rewardIcon.image = nil;
    _ctaLabel.text = @"참여 확인 중";
  } else {
    if (totalReward > 0 && isParticipated) {
      _rewardIcon.image = [UIImage imageNamed:@"ic_check"];
      _ctaLabel.text = @"참여 완료";
    } else if (reward > 0) {
      _rewardIcon.image = [UIImage imageNamed:@"ic_coin"];
      _ctaLabel.text = [NSString stringWithFormat:@"+%d %@", (int)reward, callToAction];
    } else {
      _rewardIcon.image = nil;
      _ctaLabel.text = callToAction;
    }
  }
}

- (NSString *)formattedStringForNumber:(NSNumber *)number {
  NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
  formatter.numberStyle = NSNumberFormatterDecimalStyle;
  return [formatter stringFromNumber:number];
}

#pragma mark -- BABNativeAdViewDelegate
// Handle ad callbacks
- (void)BABNativeAdView:(BABNativeAdView *)adView didImpressAd:(BABAd *)ad {
}

- (void)BABNativeAdView:(BABNativeAdView *)adView didClickAd:(BABAd *)ad {
  [self updateCtaButtonWithAd:ad];
}

- (void)BABNativeAdView:(BABNativeAdView *)adView willRequestRewardForAd:(BABAd *)ad {
}

- (void)BABNativeAdView:(BABNativeAdView *)adView didRewardForAd:(BABAd *)ad withResult:(BABRewardResult)result {
  [self updateCtaButtonWithAd:ad];
}

- (void)BABNativeAdView:(BABNativeAdView *)adView didParticipateAd:(BABAd *)ad {
  [self updateCtaButtonWithAd:ad];
}

@end
Code Block
languageobjective-c
BABFeedConfig *config = [[BABFeedConfig alloc] initWithUnitId:@"YOUR_FEED_UNIT_ID"];
config.cpsAdViewHolderClass = [CustomCpsAdViewHolder class];
Expand
titleSwift
Code Block
languageswift
class CustomCpsAdViewHolder: BABAdViewHolder {
  private var adView = BABNativeAdView()
  private var mediaView = BABMediaView()
  private var titleLabel = UILabel()
  ...

  required init?(coder: NSCoder) {
    super.init(coder: coder)
    setUpView()
  }
  
  override init(frame: CGRect) {
    super.init(frame: frame)
    setUpView()
  }
  
  func setUpView() {
    adView.delegate = self
    addSubview(self.adView)
    adView.addSubview(self.mediaView)
    adView.addSubview(self.titleLabel)
    ...
  }

  // 2) Bind view with ad
  override func renderAd(_ ad: BABAd) {
    super.renderAd(ad)
    
    self.titleLabel.text = ad.creative.title
    
    // description 필드가 objc 객체에 기본으로 있어서 body로 컨버팅 하여 사용
    self.descriptionLabel.text = ad.creative.body
    
    if let urlString = ad.creative.iconUrl {
      self.iconImageView.sd_setImage(with: URL(string: urlString))
    }
    
    // for CPS
    let product = ad.product
    if product.discountedPrice != 0 {
      self.priceView.text = NSNumber(value: product.discountedPrice).formattedString()
      self.originalPriceView.text = NSNumber(value: product.price).formattedString()
      let discountRate = (1 - product.discountedPrice / product.price) * 100
      self.discountRateView.text = String(format: "%d%%", Int(discountRate))
    } else {
      self.priceView.text = NSNumber(value: product.price).formattedString()
      self.originalPriceView.text = NSNumber(value: product.price).formattedString()
      self.discountRateView.text = "0%%"
    }
    
    if let category = product.category {
      self.categoryView.text = category
    } else {
      self.categoryView.isHidden = true
    }
    
    self.adView.ad = ad
    self.adView.mediaView = self.mediaView
    self.adView.clickableViews = [self.ctaButton, self.iconImageView]
    updateCtaButton(ad: ad)
  }
  
  func updateCtaButton(ad: BABAd) {
    let callToAction = ad.creative.callToAction ?? "디폴트 스트링"
    let reward = ad.rewardgetAvailableReward()
    let totalReward = ad.getTotalReward()
    let isParticipated = ad.isParticipated()
    let isClicked = ad.isClicked()
    let isActionType = ad.isActionType()
    if (isClicked && isActionType && !isParticipated) {
      rewardIcon.image = nil
      ctaLabel.text = "참여 확인 중"
    } else {
      if (totalReward > 0 && isParticipated) {
        rewardIcon.image = UIImage(named: "ic_check")
        ctaLabel.text = "참여 완료"
      } else if (reward > 0) {
        rewardIcon.image = UIImage(named: "ic_coin")
        ctaLabel.text = "\(Int(ad.reward))P \(callToAction)"
      } else {
        rewardIcon.image = nil
        ctaLabel.text = callToAction
      }
    }
  }
  
  // MARK: BABNativeAdViewDelegate
  // Handle ad callbacks
  func babNativeAdView(_ adView: BABNativeAdView, didImpress ad: BABAd) {
  }
  
  func babNativeAdView(_ adView: BABNativeAdView, didClick ad: BABAd) {
    updateCtaButton(ad: ad)
  }
  
  func babNativeAdView(_ adView: BABNativeAdView, willRequestRewardFor ad: BABAd) {
  }
  
  func babNativeAdView(_ adView: BABNativeAdView, didRewardFor ad: BABAd, with result: BABRewardResult) {
    updateCtaButton(ad: ad)
  }
  
  func babNativeAdView(_ adView: BABNativeAdView, didParticipateAd ad: BABAd) {
    updateCtaButton(ad: ad)
  }
}

extension NSNumber {
  func formattedString() -> String? {
    let formatter = NumberFormatter()
    formatter.numberStyle = .decimal
    return formatter.string(from: self)
  }
}
Code Block
languageswift
let config = BABFeedConfig(unitId: "YOUR_FEED_UNIT_ID")
config.cpsAdViewHolderClass = CustomCpsAdViewHolder.self

...