「Mopub Mediation Native」修訂間的差異
imported>Wikiuser |
imported>Wikiuser |
||
(未顯示同一使用者於中間所作的 12 次修訂) | |||
行 1: | 行 1: | ||
官方教學:[https:// | 官方教學:[https://developers.mopub.com/networks/integrate/build-adapters-ios/#quick-start-for-native-ads 連結]<br> | ||
NativeAd 需要建立4個客製化class來串接廣告,若是開發者原本已經有串接 MoPub native 廣告的話則只需要,TAMediaCustomNativeAdEventAdapter 及 TAMediaCustomNativeAdEvent 即可。 | |||
[[MoPub Mediation Setting|回MoPub教學]] | [[MoPub Mediation Setting|回MoPub教學]] | ||
== TAMediaCustomNativeAdEventAdapter(必要) == | == TAMediaCustomNativeAdEventAdapter(必要) == | ||
1. | 此 class 是作為 TADNativeAd 與 MPNativeAd 的橋樑,將 TADNativeAd 轉換為 MPNativeAd 所需要的 Dictionary <br> | ||
2.建立初始化方法,將TAMedia native | 1.建立一class實做 MPNativeAdAdapter protocol,protocol 有兩必須實作 property:properties 及 defaultActionURL<br> | ||
3. | 2.建立初始化方法,將TAMedia native ad作為參數帶入,將由 TAMeida native ad 拿到的 dictionary 與 MPNativeAdConstants.h 提供的 key 值建立相對應的 dictionary 後賦予給 proerties <br> | ||
4.實作<code>displayContentForURL:rootViewController:completion:</code> | 3.defaultActionURL 則為 TAmedia 拿到的 nurl<br> | ||
4.實作<code>displayContentForURL:rootViewController:completion:</code>當 user 點擊時觸發<br> | |||
參考以下代碼<br> | 參考以下代碼<br> | ||
// TAMediaCustomNativeAdEventAdapter.h | // TAMediaCustomNativeAdEventAdapter.h | ||
#import <Foundation/Foundation.h> | #import <Foundation/Foundation.h> | ||
#import | #import <MoPub/MoPub.h> | ||
#import <TAMediaAdsFramework/TAMediaAdsFramework.h> | #import <TAMediaAdsFramework/TAMediaAdsFramework.h> | ||
行 26: | 行 27: | ||
// TAMediaCustomNativeAdEventAdapter.m | // TAMediaCustomNativeAdEventAdapter.m | ||
#import "TAMediaCustomNativeAdEventAdapter.h" | #import "TAMediaCustomNativeAdEventAdapter.h" | ||
@interface TAMediaCustomNativeAdEventAdapter () <TADNativeAdDelegate> | @interface TAMediaCustomNativeAdEventAdapter () <TADNativeAdDelegate> | ||
行 67: | 行 67: | ||
- (void)displayContentForURL:(NSURL *)URL rootViewController:(UIViewController *)controller { | - (void)displayContentForURL:(NSURL *)URL rootViewController:(UIViewController *)controller { | ||
[self.nativeAd handleClick]; | |||
if ([self.delegate respondsToSelector:@selector(nativeAdDidClick:)]) { | |||
[self.delegate nativeAdDidClick:self]; | |||
} | |||
} | } | ||
- (void)adViewWillLeaveApplication:(TADNativeAd *)adView { | - (void)adViewWillLeaveApplication:(TADNativeAd *)adView { | ||
行 80: | 行 80: | ||
== TAMediaCustomNativeAdEventSetting == | == TAMediaCustomNativeAdEventSetting == | ||
此class需實做MPNativeAdRendererSettings protocol,用來設定 | 此class需實做MPNativeAdRendererSettings protocol,用來設定 viewSize,與指定需要來呈現廣告的 View,亦可使用 MPNativeAdRendererSettings 取代<br> | ||
// TAMediaCustomNativeAdEventSetting.h | // TAMediaCustomNativeAdEventSetting.h | ||
#import <Foundation/Foundation.h> | #import <Foundation/Foundation.h> | ||
#import | #import <MoPub/MoPub.h> | ||
@interface TAMediaCustomNativeAdEventSetting : NSObject <MPNativeAdRendererSettings> | @interface TAMediaCustomNativeAdEventSetting : NSObject <MPNativeAdRendererSettings> | ||
行 95: | 行 95: | ||
@implementation TAMediaCustomNativeAdEventSetting | @implementation TAMediaCustomNativeAdEventSetting | ||
@end | @end | ||
== TAMediaCustomNativeAdRenderer == | == TAMediaCustomNativeAdRenderer == | ||
此 class 需實做 MPNativeAdRenderer protocol,用於設置客製化事件處理的 class (TAMediaCustomNativeAdEvent) 以及將由 adapter 取得的 Dictionary 呈現到所需的 adView 內並回傳<br> | |||
1.實作<code>+ (MPNativeAdRendererConfiguration *)rendererConfigurationWithRendererSettings:(id<MPNativeAdRendererSettings>)rendererSettings</code> | 1.實作<code>+ (MPNativeAdRendererConfiguration *)rendererConfigurationWithRendererSettings:(id<MPNativeAdRendererSettings>)rendererSettings</code>建立 MPNativeAdRendererConfiguration物件<br> | ||
2. | 2.設置 rendering class 為本身class<br> | ||
3. | 3.設置 rendererSettings 為參數傳入 的rendererSettings<br> | ||
4. | 4.設置 supportedCustomEvents 為 TAMediaCustomNativeAdEvent class<br> | ||
5.實作<code>initWithRendererSettings:</code>設置物件viewSizeHandler,以及任何客製化設定<br> | 5.實作<code>initWithRendererSettings:</code>設置物件viewSizeHandler,以及任何客製化設定<br> | ||
6.實作<code>retrieveViewWithAdapter:</code> | 6.實作<code>retrieveViewWithAdapter:</code>回傳要來呈顯在畫面上的 View<br> | ||
參考以下代碼<br> | 參考以下代碼<br> | ||
// TAMediaCustomNativeAdRenderer.h | // TAMediaCustomNativeAdRenderer.h | ||
#import <Foundation/Foundation.h> | #import <Foundation/Foundation.h> | ||
#import | #import <MoPub/MoPub.h> | ||
@interface TAMediaCustomNativeAdRenderer : NSObject <MPNativeAdRenderer> | @interface TAMediaCustomNativeAdRenderer : NSObject <MPNativeAdRenderer> | ||
行 118: | 行 117: | ||
// TAMediaCustomNativeAdRenderer.m | // TAMediaCustomNativeAdRenderer.m | ||
#import "TAMediaCustomNativeAdRenderer.h" | #import "TAMediaCustomNativeAdRenderer.h" | ||
#import "MPNativeAdView.h" | #import "MPNativeAdView.h" | ||
@interface TAMediaCustomNativeAdRenderer() <MPNativeAdRendererImageHandlerDelegate> | @interface TAMediaCustomNativeAdRenderer() <MPNativeAdRendererImageHandlerDelegate> | ||
行 177: | 行 169: | ||
== TAMediaCustomNativeAdEvent(必要) == | == TAMediaCustomNativeAdEvent(必要) == | ||
此 class 需繼承 MPNativeCustomEvent,用來取得 TAmedia native ad 提供給 MoPub<br> | |||
1. | 1.建立 class 繼承 MPNativeCustomEvent<br> | ||
2.複寫<code> - (void)requestAdWithCustomEventInfo:(NSDictionary *)info</code> | 2.複寫<code> - (void)requestAdWithCustomEventInfo:(NSDictionary *)info</code>在此用 TADNativeAd 去取得廣告<br> | ||
3. | 3.取得廣告後用取得的 Dictionary 建立 TAMediaCustomNativeAdEventAdapter 後再用 adapter 建立MPNativeAd<br> | ||
4.呈現廣告前先呼叫父類別<code>precacheImagesWithURLs:completionBlock:<code/>取得image後,再呼叫<code>customNativeEvent:didLoadAd:</code><br> | 4.呈現廣告前先呼叫父類別<code>precacheImagesWithURLs:completionBlock:<code/>取得image後,再呼叫<code>customNativeEvent:didLoadAd:</code><br> | ||
#import <MoPub/MoPub.h> | |||
#import <TAMediaAdsFramework/TAMediaAdsFramework.h> | |||
@interface TAMediaCustomNativeAdEvent : MPNativeCustomEvent <TADNativeAdDelegate> { | |||
TADNativeAd *nativeAd; | |||
} | |||
@end | |||
#import "TAMediaCustomNativeAdEvent.h" | |||
#import "TAMediaCustomNativeAdEventAdapter.h" | |||
#import <MoPub/MoPub.h> | |||
@implementation TAMediaCustomNativeAdEvent | |||
- (void)requestAdWithCustomEventInfo:(NSDictionary *)info adMarkup:(NSString *)adMarkup { | |||
TADRequest *request = [TADRequest request]; | |||
request.testing = NO; | |||
// Type: TADGender | |||
request.gender = kTADGenderUnknown; | |||
// NAD 物件初始化, 帶入自訂的origin | |||
if (!nativeAd) { | |||
nativeAd = [[TADNativeAd alloc] initWithAdUnitId:info[@"NativeId"]]; | |||
} | |||
// 必須要設定delegate | |||
nativeAd.delegate = self; | |||
// 載入廣告 | |||
[nativeAd loadRequest:request]; | |||
} | |||
- (void)nativeAdDidReceiveAd:(TADNativeAd *)ad { | |||
// 表示本次請求收到廣告 | |||
TAMediaCustomNativeAdEventAdapter *adapter = [[TAMediaCustomNativeAdEventAdapter alloc] initWithTADNativeAd:ad]; | |||
MPNativeAd *mpNativeAd = [[MPNativeAd alloc] initWithAdAdapter:adapter]; | |||
//藉由 getNativeAdContent 取得包含回覆內容的 NSDictionary | |||
NSDictionary *nadDic = [nativeAd getNativeAdContent]; | |||
NSMutableArray *imageURLs = [NSMutableArray array]; | |||
for (NSString *key in nadDic.allKeys) { | |||
if ([[key lowercaseString] hasPrefix:@"image"] || [key hasPrefix:@"ICON"]) { | |||
if (nadDic[key] && ![nadDic[key] isEqualToString:@""]) { | |||
NSURL *url = [NSURL URLWithString:nadDic[key]]; | |||
[imageURLs addObject:url]; | |||
} | |||
} | |||
} | |||
//在呼叫delegate didloadAd前先將圖片預載 | |||
[super precacheImagesWithURLs:imageURLs completionBlock:^(NSArray *errors) { | |||
if (errors) { | |||
[self.delegate nativeCustomEvent:self didFailToLoadAdWithError:MPNativeAdNSErrorForImageDownloadFailure()]; | |||
} else { | |||
[self.delegate nativeCustomEvent:self didLoadAd:mpNativeAd]; | |||
} | |||
}]; | |||
} | |||
- (void)nativeAd:(TADNativeAd *)ad didFailToReceiveAdWithError:(TADRequestError *)error { | |||
if ([self.delegate respondsToSelector:@selector(nativeCustomEvent:didFailToLoadAdWithError:)]) { | |||
[self.delegate nativeCustomEvent:self didFailToLoadAdWithError:error]; | |||
} | |||
} | |||
- (void)dealloc { | |||
if (nativeAd != nil) { | |||
nativeAd.delegate = nil; | |||
nativeAd = nil; | |||
} | |||
} | |||
@end | |||
於 2021年5月19日 (三) 09:44 的最新修訂
官方教學:連結
NativeAd 需要建立4個客製化class來串接廣告,若是開發者原本已經有串接 MoPub native 廣告的話則只需要,TAMediaCustomNativeAdEventAdapter 及 TAMediaCustomNativeAdEvent 即可。
TAMediaCustomNativeAdEventAdapter(必要)
此 class 是作為 TADNativeAd 與 MPNativeAd 的橋樑,將 TADNativeAd 轉換為 MPNativeAd 所需要的 Dictionary
1.建立一class實做 MPNativeAdAdapter protocol,protocol 有兩必須實作 property:properties 及 defaultActionURL
2.建立初始化方法,將TAMedia native ad作為參數帶入,將由 TAMeida native ad 拿到的 dictionary 與 MPNativeAdConstants.h 提供的 key 值建立相對應的 dictionary 後賦予給 proerties
3.defaultActionURL 則為 TAmedia 拿到的 nurl
4.實作displayContentForURL:rootViewController:completion:
當 user 點擊時觸發
參考以下代碼
// TAMediaCustomNativeAdEventAdapter.h #import <Foundation/Foundation.h> #import <MoPub/MoPub.h> #import <TAMediaAdsFramework/TAMediaAdsFramework.h> @interface TAMediaCustomNativeAdEventAdapter : NSObject <MPNativeAdAdapter> - (instancetype)initWithTADNativeAd:(TADNativeAd *)nativeAd; @property (nonatomic, readonly) NSDictionary *properties; @property (nonatomic, readonly) NSURL *defaultActionURL; @property (nonatomic, weak) id<MPNativeAdAdapterDelegate> delegate; @end
// TAMediaCustomNativeAdEventAdapter.m #import "TAMediaCustomNativeAdEventAdapter.h" @interface TAMediaCustomNativeAdEventAdapter () <TADNativeAdDelegate> @property (nonatomic,strong) TADNativeAd *nativeAd; @end @implementation TAMediaCustomNativeAdEventAdapter - (instancetype)initWithTADNativeAd:(TADNativeAd *)nativeAd { self = [super init]; NSDictionary *tadDic = [nativeAd getNativeAdContent]; nativeAd.delegate = self; self.nativeAd = nativeAd; self = [self initWithTADic:tadDic]; return self; } - (instancetype)initWithTADic:(NSDictionary *)taDic { self = [super init]; NSDictionary *taMeidaDicMapping = @{@"LONGSUBJECT":kAdTitleKey, @"BODY":kAdTextKey, @"ICONSQUARE":kAdIconImageKey, @"IMAGE1200X627":kAdMainImageKey, @"VIDEO":kVASTVideoKey, @"nurl":kDefaultActionURLKey, @"CTA":kAdCTATextKey }; NSMutableDictionary *dic = [NSMutableDictionary new]; for (NSString *key in taDic.allKeys) { NSString *newKey = taMeidaDicMapping[key]; if (newKey == nil) { newKey = key; } [dic setObject:taDic[key] forKey:newKey]; } _properties = dic; _defaultActionURL = [NSURL URLWithString:taDic[@"nurl"]]; return self; } - (void)displayContentForURL:(NSURL *)URL rootViewController:(UIViewController *)controller { [self.nativeAd handleClick]; if ([self.delegate respondsToSelector:@selector(nativeAdDidClick:)]) { [self.delegate nativeAdDidClick:self]; } } - (void)adViewWillLeaveApplication:(TADNativeAd *)adView { if ([self.delegate respondsToSelector:@selector(nativeAdWillLeaveApplicationFromAdapter:)]) { [self.delegate nativeAdWillLeaveApplicationFromAdapter:self]; } } @end
TAMediaCustomNativeAdEventSetting
此class需實做MPNativeAdRendererSettings protocol,用來設定 viewSize,與指定需要來呈現廣告的 View,亦可使用 MPNativeAdRendererSettings 取代
// TAMediaCustomNativeAdEventSetting.h #import <Foundation/Foundation.h> #import <MoPub/MoPub.h> @interface TAMediaCustomNativeAdEventSetting : NSObject <MPNativeAdRendererSettings> @property (nonatomic, assign) Class renderingViewClass; @property (nonatomic, readwrite, copy) MPNativeViewSizeHandler viewSizeHandler; @end
// TAMediaCustomNativeAdEventSetting.m #import "TAMediaCustomNativeAdEventSetting.h" @implementation TAMediaCustomNativeAdEventSetting @end
TAMediaCustomNativeAdRenderer
此 class 需實做 MPNativeAdRenderer protocol,用於設置客製化事件處理的 class (TAMediaCustomNativeAdEvent) 以及將由 adapter 取得的 Dictionary 呈現到所需的 adView 內並回傳
1.實作+ (MPNativeAdRendererConfiguration *)rendererConfigurationWithRendererSettings:(id<MPNativeAdRendererSettings>)rendererSettings
建立 MPNativeAdRendererConfiguration物件
2.設置 rendering class 為本身class
3.設置 rendererSettings 為參數傳入 的rendererSettings
4.設置 supportedCustomEvents 為 TAMediaCustomNativeAdEvent class
5.實作initWithRendererSettings:
設置物件viewSizeHandler,以及任何客製化設定
6.實作retrieveViewWithAdapter:
回傳要來呈顯在畫面上的 View
參考以下代碼
// TAMediaCustomNativeAdRenderer.h #import <Foundation/Foundation.h> #import <MoPub/MoPub.h> @interface TAMediaCustomNativeAdRenderer : NSObject <MPNativeAdRenderer> @property (nonatomic, readonly) MPNativeViewSizeHandler viewSizeHandler; @end
// TAMediaCustomNativeAdRenderer.m #import "TAMediaCustomNativeAdRenderer.h" #import "MPNativeAdView.h" @interface TAMediaCustomNativeAdRenderer() <MPNativeAdRendererImageHandlerDelegate> @property (nonatomic,strong) id<MPNativeAdRendererSettings>setting; @property (nonatomic,strong) MPNativeAdRendererImageHandler *rendererImageHandler; @end @implementation TAMediaCustomNativeAdRenderer + (MPNativeAdRendererConfiguration *)rendererConfigurationWithRendererSettings:(id<MPNativeAdRendererSettings>)rendererSettings { MPNativeAdRendererConfiguration *config = [[MPNativeAdRendererConfiguration alloc] init]; config.rendererSettings = rendererSettings; config.supportedCustomEvents = @[@"TAMediaCustomNativeAdEvent"];//設置客製化事件處理的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設置的項目 //需依照串接不同的Native廣告,設定不同的值 adView.nativeTitleTextLabel.text = adapter.properties[kAdTitleKey]; adView.nativeMainTextLabel.text = adapter.properties[kAdTextKey]; adView.nativeCallToActionTextLabel.text = adapter.properties[kAdCTATextKey]; [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
TAMediaCustomNativeAdEvent(必要)
此 class 需繼承 MPNativeCustomEvent,用來取得 TAmedia native ad 提供給 MoPub
1.建立 class 繼承 MPNativeCustomEvent
2.複寫 - (void)requestAdWithCustomEventInfo:(NSDictionary *)info
在此用 TADNativeAd 去取得廣告
3.取得廣告後用取得的 Dictionary 建立 TAMediaCustomNativeAdEventAdapter 後再用 adapter 建立MPNativeAd
4.呈現廣告前先呼叫父類別precacheImagesWithURLs:completionBlock:
取得image後,再呼叫
customNativeEvent:didLoadAd:
#import <MoPub/MoPub.h>
#import <TAMediaAdsFramework/TAMediaAdsFramework.h>
@interface TAMediaCustomNativeAdEvent : MPNativeCustomEvent <TADNativeAdDelegate> {
TADNativeAd *nativeAd;
}
@end
#import "TAMediaCustomNativeAdEvent.h"
#import "TAMediaCustomNativeAdEventAdapter.h"
#import <MoPub/MoPub.h>
@implementation TAMediaCustomNativeAdEvent
- (void)requestAdWithCustomEventInfo:(NSDictionary *)info adMarkup:(NSString *)adMarkup {
TADRequest *request = [TADRequest request];
request.testing = NO;
// Type: TADGender
request.gender = kTADGenderUnknown;
// NAD 物件初始化, 帶入自訂的origin
if (!nativeAd) {
nativeAd = [[TADNativeAd alloc] initWithAdUnitId:info[@"NativeId"]];
}
// 必須要設定delegate
nativeAd.delegate = self;
// 載入廣告
[nativeAd loadRequest:request];
}
- (void)nativeAdDidReceiveAd:(TADNativeAd *)ad {
// 表示本次請求收到廣告
TAMediaCustomNativeAdEventAdapter *adapter = [[TAMediaCustomNativeAdEventAdapter alloc] initWithTADNativeAd:ad];
MPNativeAd *mpNativeAd = [[MPNativeAd alloc] initWithAdAdapter:adapter];
//藉由 getNativeAdContent 取得包含回覆內容的 NSDictionary
NSDictionary *nadDic = [nativeAd getNativeAdContent];
NSMutableArray *imageURLs = [NSMutableArray array];
for (NSString *key in nadDic.allKeys) {
if ([[key lowercaseString] hasPrefix:@"image"] || [key hasPrefix:@"ICON"]) {
if (nadDic[key] && ![nadDic[key] isEqualToString:@""]) {
NSURL *url = [NSURL URLWithString:nadDic[key]];
[imageURLs addObject:url];
}
}
}
//在呼叫delegate didloadAd前先將圖片預載
[super precacheImagesWithURLs:imageURLs completionBlock:^(NSArray *errors) {
if (errors) {
[self.delegate nativeCustomEvent:self didFailToLoadAdWithError:MPNativeAdNSErrorForImageDownloadFailure()];
} else {
[self.delegate nativeCustomEvent:self didLoadAd:mpNativeAd];
}
}];
}
- (void)nativeAd:(TADNativeAd *)ad didFailToReceiveAdWithError:(TADRequestError *)error {
if ([self.delegate respondsToSelector:@selector(nativeCustomEvent:didFailToLoadAdWithError:)]) {
[self.delegate nativeCustomEvent:self didFailToLoadAdWithError:error];
}
}
- (void)dealloc {
if (nativeAd != nil) {
nativeAd.delegate = nil;
nativeAd = nil;
}
}
@end