「Mopub Mediation Native」修訂間的差異

出自TAMedia
跳至導覽 跳至搜尋
imported>Wikiuser
(新頁面: 官方教學:[https://github.com/mopub/mopub-ios-sdk/wiki/Custom-Events 連結]<br> *Step1. 1.建立一class實做MPNativeAdAdapter protocol,protocol有兩必須實作property:prop...)
 
imported>Wikiuser
行 1: 行 1:
官方教學:[https://github.com/mopub/mopub-ios-sdk/wiki/Custom-Events 連結]<br>
官方教學:[https://github.com/mopub/mopub-ios-sdk/wiki/Custom-Events 連結]<br>
*Step1.
NativeAd需要建立4個客製化class
== CustomNativeAdEventAdapter ==
1.建立一class實做MPNativeAdAdapter protocol,protocol有兩必須實作property:properties及defaultActionURL,此class是作為Tamedia native ad與MPNativeAd的橋樑<br>
1.建立一class實做MPNativeAdAdapter protocol,protocol有兩必須實作property:properties及defaultActionURL,此class是作為Tamedia native ad與MPNativeAd的橋樑<br>
2.建立初始化方法,將Tamedia native ad拿到的dictionary作為參數帶入,將帶入的dictionary與MPNativeAdConstants.h提供的key值建立相對應的dictionary後賦予給prooerties<br>
2.建立初始化方法,將Tamedia native ad拿到的dictionary作為參數帶入,將帶入的dictionary與MPNativeAdConstants.h提供的key值建立相對應的dictionary後賦予給prooerties<br>
行 6: 行 7:
4.實作<code>displayContentForURL:rootViewController:completion:</code>當user點擊時觸發<br>
4.實作<code>displayContentForURL:rootViewController:completion:</code>當user點擊時觸發<br>
參考以下代碼<br>
參考以下代碼<br>
 
  //  CustomNativeAdEventAdapter.h
== CustomNativeAdEventAdapter ==
   #import <Foundation/Foundation.h>
   #import <Foundation/Foundation.h>
   #import "MPNativeAdAdapter.h"
   #import "MPNativeAdAdapter.h"
行 20: 行 20:
   @end
   @end


  //  CustomNativeAdEventAdapter.m
   #import "CustomNativeAdEventAdapter.h"
   #import "CustomNativeAdEventAdapter.h"
   #import "MPNativeAdConstants.h"
   #import "MPNativeAdConstants.h"
行 54: 行 55:
   - (void)displayContentForURL:(NSURL *)URL rootViewController:(UIViewController *)controller {     
   - (void)displayContentForURL:(NSURL *)URL rootViewController:(UIViewController *)controller {     
       [[UIApplication sharedApplication] openURL:URL];
       [[UIApplication sharedApplication] openURL:URL];
  }
  @end
== CustomNativeAdEventSetting ==
CustomNativeAdEventSetting用來設定 viewSize,與指定需要來呈現的View
  //  CustomNativeAdEventSetting.h
  #import <Foundation/Foundation.h>
  #import "MPNativeAdRendererSettings.h"
  @interface CustomNativeAdEventSetting : NSObject <MPNativeAdRendererSettings>
 
  @property (nonatomic, assign) Class renderingViewClass;
 
  @property (nonatomic, readwrite, copy) MPNativeViewSizeHandler viewSizeHandler;
  @end
  //  CustomNativeAdEventSetting.m
  #import "CustomNativeAdEventSetting.h"
  @implementation CustomNativeAdEventSetting
  @end
== CustomNativeAdRenderer ==
用於回傳所需要的AdView以及設置客製化事件處理的class<br>
  //  CustomNativeAdRenderer.h
  #import <Foundation/Foundation.h>
  #import "MPNativeAdRenderer.h"
  @interface CustomNativeAdRenderer : NSObject <MPNativeAdRenderer>
 
  @property (nonatomic, readonly) MPNativeViewSizeHandler viewSizeHandler;
 
  @end
  //  CustomNativeAdRenderer.m
  #import "CustomNativeAdRenderer.h"
  #import "MPNativeAdRendererConfiguration.h"
  #import "MPNativeAdRendererSettings.h"
  #import "MPNativeAdRendering.h"
  #import "MPNativeAdAdapter.h"
  #import "MPNativeAdConstants.h"
  #import "MPNativeAdView.h"
  #import "MPNativeAdRendererImageHandler.h"
  #import "MPNativeAdRenderingImageLoader.h"
 
  @interface CustomNativeAdRenderer() <MPNativeAdRendererImageHandlerDelegate>
 
  @property (nonatomic,strong) id<MPNativeAdRendererSettings>setting;
 
  @property (nonatomic,strong) MPNativeAdRendererImageHandler *rendererImageHandler;
 
  @end
   
  @implementation CustomNativeAdRenderer
 
  + (MPNativeAdRendererConfiguration *)rendererConfigurationWithRendererSettings:(id<MPNativeAdRendererSettings>)rendererSettings {
      MPNativeAdRendererConfiguration *config = [[MPNativeAdRendererConfiguration alloc] init];
      config.rendererSettings = rendererSettings;
      config.supportedCustomEvents = @[@"CustomNativeAdEvent"];'''//設置客製化事件處理的class'''
      config.rendererClass = [self class];
      return config;
  }
 
  - (instancetype)initWithRendererSettings:(id<MPNativeAdRendererSettings>)rendererSettings {
      self = [super init];
      _viewSizeHandler = [rendererSettings.viewSizeHandler copy];
      self.setting = rendererSettings;
      self.rendererImageHandler = [MPNativeAdRendererImageHandler new];
      self.rendererImageHandler.delegate = self;
      return self;
  }
 
  - (UIView *)retrieveViewWithAdapter:(id<MPNativeAdAdapter>)adapter error:(NSError **)error {
      UIView<MPNativeAdRendering> *adView = [MPNativeAdView new];
      '''//MPNativeAdRendering potocol設置的項目'''
      adView.nativeTitleTextLabel.text = adapter.properties[kAdTitleKey];
      adView.nativeMainTextLabel.text = adapter.properties[kAdTextKey];
      [self.rendererImageHandler loadImageForURL:[NSURL URLWithString:[adapter.properties objectForKey:kAdIconImageKey]] intoImageView:adView.nativeIconImageView];
      [self.rendererImageHandler loadImageForURL:[NSURL URLWithString:[adapter.properties objectForKey:kAdMainImageKey]] intoImageView:adView.nativeMainImageView];
     
      '''//MPNativeAdRendering potocol以外的項目用以下方式設置'''
      MPNativeAdRenderingImageLoader *imageLoader = [[MPNativeAdRenderingImageLoader alloc] initWithImageHandler:self.rendererImageHandler];
      [adView layoutCustomAssetsWithProperties:adapter.properties imageLoader:imageLoader];
      return adView;
  }
 
  - (BOOL)nativeAdViewInViewHierarchy
  {
      return YES;
   }
   }
   @end
   @end

於 2017年6月7日 (三) 09:46 的修訂

官方教學:連結
NativeAd需要建立4個客製化class

CustomNativeAdEventAdapter

1.建立一class實做MPNativeAdAdapter protocol,protocol有兩必須實作property:properties及defaultActionURL,此class是作為Tamedia native ad與MPNativeAd的橋樑
2.建立初始化方法,將Tamedia native ad拿到的dictionary作為參數帶入,將帶入的dictionary與MPNativeAdConstants.h提供的key值建立相對應的dictionary後賦予給prooerties
3.defaultActionURL則為Tamedia拿到的nurl
4.實作displayContentForURL:rootViewController:completion:當user點擊時觸發
參考以下代碼

  //  CustomNativeAdEventAdapter.h
  #import <Foundation/Foundation.h>
  #import "MPNativeAdAdapter.h"
  #import "TADNativeAd.h"
  
  @interface CustomNativeAdEventAdapter : NSObject <MPNativeAdAdapter>
  
  - (instancetype)initWithTADic:(NSDictionary *)taDic;
  
  @property (nonatomic, readonly) NSDictionary *properties;
  @property (nonatomic, readonly) NSURL *defaultActionURL;
  @end
  //  CustomNativeAdEventAdapter.m
  #import "CustomNativeAdEventAdapter.h"
  #import "MPNativeAdConstants.h"
  
  @interface CustomNativeAdEventAdapter ()   
  @end
  
  @implementation CustomNativeAdEventAdapter 
  
  - (instancetype)initWithTADic:(NSDictionary *)taDic {
      self = [super init];
      NSDictionary *taMeidaDicMapping = @{@"LONGSUBJECT":kAdTitleKey,
                                          @"BODY":kAdTextKey,
                                          @"ICONSQUARE":kAdIconImageKey,
                                          @"IMAGE1280X720":kAdMainImageKey,
                                          @"VIDEO":kVASTVideoKey,
                                          @"nurl":kDefaultActionURLKey,
                                          @"IMAGE960X640":@"IMAGE960X640",
                                          @"IMAGE1200X627":@"IMAGE1200X627",
                                          @"ICONRECTANGLE":@"ICONRECTANGLE",
                                          @"SHORTSUBJECT":@"SHORTSUBJECT",
                                          @"IMAGE720X1280":@"IMAGE720X1280",
                                          @"IMAGE640X960":@"IMAGE640X960"};
      
      NSMutableDictionary *dic = [NSMutableDictionary new];
      for (NSString *key in taDic.allKeys) {
          [dic setObject:taDic[key] forKey:taMeidaDicMapping[key]];
      }
      _properties = dic;
      _defaultActionURL = [NSURL URLWithString:taDic[@"nurl"]];
      return self;
  }
  
  - (void)displayContentForURL:(NSURL *)URL rootViewController:(UIViewController *)controller {    
      [[UIApplication sharedApplication] openURL:URL];
  }
  @end

CustomNativeAdEventSetting

CustomNativeAdEventSetting用來設定 viewSize,與指定需要來呈現的View

  //  CustomNativeAdEventSetting.h
  #import <Foundation/Foundation.h>
  #import "MPNativeAdRendererSettings.h"
  @interface CustomNativeAdEventSetting : NSObject <MPNativeAdRendererSettings>
  
  @property (nonatomic, assign) Class renderingViewClass;
  
  @property (nonatomic, readwrite, copy) MPNativeViewSizeHandler viewSizeHandler;
  @end
  //  CustomNativeAdEventSetting.m
  #import "CustomNativeAdEventSetting.h"
  @implementation CustomNativeAdEventSetting
  @end


CustomNativeAdRenderer

用於回傳所需要的AdView以及設置客製化事件處理的class

  //  CustomNativeAdRenderer.h
  #import <Foundation/Foundation.h>
  #import "MPNativeAdRenderer.h"
  @interface CustomNativeAdRenderer : NSObject <MPNativeAdRenderer>
  
  @property (nonatomic, readonly) MPNativeViewSizeHandler viewSizeHandler;
  
  @end
  //  CustomNativeAdRenderer.m
  #import "CustomNativeAdRenderer.h"
  #import "MPNativeAdRendererConfiguration.h"
  #import "MPNativeAdRendererSettings.h"
  #import "MPNativeAdRendering.h"
  #import "MPNativeAdAdapter.h"
  #import "MPNativeAdConstants.h"
  #import "MPNativeAdView.h"
  #import "MPNativeAdRendererImageHandler.h"
  #import "MPNativeAdRenderingImageLoader.h"
  
  @interface CustomNativeAdRenderer() <MPNativeAdRendererImageHandlerDelegate>
  
  @property (nonatomic,strong) id<MPNativeAdRendererSettings>setting;
  
  @property (nonatomic,strong) MPNativeAdRendererImageHandler *rendererImageHandler;
  
  @end
   
  @implementation CustomNativeAdRenderer
  
  + (MPNativeAdRendererConfiguration *)rendererConfigurationWithRendererSettings:(id<MPNativeAdRendererSettings>)rendererSettings {
      MPNativeAdRendererConfiguration *config = [[MPNativeAdRendererConfiguration alloc] init];
      config.rendererSettings = rendererSettings;
      config.supportedCustomEvents = @[@"CustomNativeAdEvent"];//設置客製化事件處理的class
      config.rendererClass = [self class];
      return config;
  }
  
  - (instancetype)initWithRendererSettings:(id<MPNativeAdRendererSettings>)rendererSettings {
      self = [super init];
      _viewSizeHandler = [rendererSettings.viewSizeHandler copy];
      self.setting = rendererSettings;
      self.rendererImageHandler = [MPNativeAdRendererImageHandler new];
      self.rendererImageHandler.delegate = self;
      return self;
  }
  
  - (UIView *)retrieveViewWithAdapter:(id<MPNativeAdAdapter>)adapter error:(NSError **)error {
      UIView<MPNativeAdRendering> *adView = [MPNativeAdView new];
      //MPNativeAdRendering potocol設置的項目
      adView.nativeTitleTextLabel.text = adapter.properties[kAdTitleKey];
      adView.nativeMainTextLabel.text = adapter.properties[kAdTextKey];
      [self.rendererImageHandler loadImageForURL:[NSURL URLWithString:[adapter.properties objectForKey:kAdIconImageKey]] intoImageView:adView.nativeIconImageView];
      [self.rendererImageHandler loadImageForURL:[NSURL URLWithString:[adapter.properties objectForKey:kAdMainImageKey]] intoImageView:adView.nativeMainImageView];
      
      //MPNativeAdRendering potocol以外的項目用以下方式設置
      MPNativeAdRenderingImageLoader *imageLoader = [[MPNativeAdRenderingImageLoader alloc] initWithImageHandler:self.rendererImageHandler];
      [adView layoutCustomAssetsWithProperties:adapter.properties imageLoader:imageLoader];
      return adView;
  }
  
  - (BOOL)nativeAdViewInViewHierarchy
  {
      return YES;
  }
  @end