一:前提条件
1:将KeyMob与应用集成。
2:您可能还需要先了解发送AdRequest的相关信息以及中介的工作原理。
二:横幅广告自定义事件
在以下示例中,您将首先在KeyMob中介内创建一个横幅广告自定义事件。这需要通过KeyMob界面定义一个自定义事件,指向您应用中的特定类,然后实现自定义事件横幅广告以投放视图。
三:定义自定义事件
自定义事件必须在 KeyMob界面中进行定义。您可以在此帮助中心指南中找到为特定广告单元修改中介的说明。
以下是自定义事件示例的部分设置:
Label:AdMobCustomEvent
Class Name:CustomAd
Parameter:a1401234567890
要定义自定义事件,您仅需提供类的名称作为类名称即可。参数应该包含所有必需的信息,以便向广告网络发送您在自定义事件中实现的广告请求。在这种情况下,KeyMob自定义事件仅需要发布商ID。
四:请求横幅广告
定义名为CustomAd的类,它表示在您的自定义事件设置中定义的类名称。它应该实现GADCustomEventBanner。当系统从中介广告瀑布流中选择自定义事件后,KeyMob中介 SDK 会针对您在设置中提供的类名称调用requestBannerAd方法。您可以使用此方法提供的参数向您所需的广告网络发送横幅广告请求。以下示例向 KeyMob发出了请求。
1:CustomAd.h
@import GoogleMobileAds;
@interface CustomAd:NSObject<GADCustomEventBanner,GADBannerViewDelegate> {
GADBannerView *bannerView_;
}
@end
2:CustomAd.m
#import "CustomAd.h"
@implementation CustomAd
// Will be set by the Mobile Ads SDK.
@synthesize delegate;
- (void)dealloc {
bannerView_.delegate = nil;
[bannerView_ release];
[super dealloc];
}
#pragma mark -
#pragma mark GADCustomEventBanner
- (void)requestBannerAd:(GADAdSize)adSize
parameter:(NSString *)serverParameter
label:(NSString *)serverLabel
request:(GADCustomEventRequest *)customEventRequest {
// Create the bannerView with the appropriate size.
self.bannerView = [[GADBannerView alloc] initWithAdSize:adSize];
// Set the delegate to listen for callbacks.
self.bannerView.delegate = self;
// Set the publisher ID for the banner. This comes from server parameter
// you provide when creating the custom event in mediation.
self.bannerView.adUnitID = serverParameter;
// Let the bannerView know which UIViewController to restore after returning
// from and ad click. The UIViewController is available from
// GADCustomEventBannerDelegate.
self.bannerView.rootViewController=[self.delegate viewControllerForPresentingModalView ];
// Create an ad request using custom targeting options from the custom event
// request
GADRequest *request = [GADRequest request];
[request setAdditionalParameters:[customEventRequest additionalParameters]];
[request setBirthday:[customEventRequest userBirthday]];
[request setGender:[customEventRequest userGender]];
[request setTesting:[customEventRequest isTesting]];
if ([customEventRequest userHasLocation]) {
[request setLocationWithLatitude:[customEventRequest userLatitude]
longitude:[customEventRequest userLongitude]
accuracy:[customEventRequest userLocationAccuracyInMeters]];
}
[self.bannerView loadRequest:request];
}
自定义事件必须在成功收到广告或无法收到广告时,通过GADCustomEventBanner通知中介 SDK。否则,自定义事件会超时,中介会继续联系下一个广告网络。
五:通知 KeyMob中介
为您的广告网络实施广告监听器并调用GADCustomEventBanner上的相关回调,以将消息发回给中介。以下示例会实施KeyMob的GADBannerViewDelegate接口,以发送这些消息。
#pragma mark -
#pragma mark GADBannerView Callbacks
- (void)adViewDidReceiveAd:(GADBannerView *)adView {
[self.delegate customEventBanner:self didReceiveAd:adView];
}
- (void)adView:(GADBannerView *)view
didFailToReceiveAdWithError:(NSError *)error {
[self.delegate customEventBanner:self didFailAd:error];
}
- (void)adViewWillPresentScreen:(GADBannerView *)adView {
[self.delegate customEventBanner:self clickDidOccurInAd:adView];
[self.delegate customEventBannerWillPresentModal:self];
}
- (void)adViewWillDismissScreen:(GADBannerView *)adView {
[self.delegate customEventBannerWillDismissModal:self];
}
- (void)adViewDidDismissScreen:(GADBannerView *)adView {
[self.delegate customEventBannerDidDismissModal:self];
}
- (void)adViewWillLeaveApplication:(GADBannerView *)adView {
[self.delegate customEventBannerWillLeaveApplication:self];
}
@end
注意:您必须将所有的回调都通知中介。
您的自定义事件至少应调用GADCustomEventBanner中的以下三种回调:
customEventBanner:didReceiveAd:中介可以展示传递给它的广告。无法调用此回调会导致超时,从而使中介继续联系下一个广告网络。
customEventBanner:didFailAd:中介可以继续从中介广告瀑布流中的下一个广告网络中请求广告,而不必等待超时。
customEventBanner:clickDidOccurInAd:让中介记录并报告您的自定义事件收到的点击次数。
五:插页式广告自定义事件
插页式广告自定义事件的实现方法与横幅广告自定义事件的实现方法类似。两者的主要区别是:您创建的插页式广告自定义事件类应该实现GADCustomEventInterstitial接口(而不是GADCustomEventBanner)。
六:请求插页式广告
以下示例介绍了通过自定义事件请求KeyMob 插页式广告的方法:
1:CustomAd.h
@import GoogleMobileAds;
@interface CustomAd:NSObject<GADCustomEventInterstitial,GADInterstitialDelegate> {
GADInterstitial *interstitial_;
}
@end
2:CustomAd.m
#import "CustomAd.h"
@implementation CustomAd
// Will be set by the Mobile Ads SDK.
@synthesize delegate;
- (void)dealloc {
interstitial_.delegate = nil;
[interstitial_ release];
[super dealloc];
}
#pragma mark -
#pragma mark GADCustomEventInterstitial
- (void)requestInterstitialAdWithParameter:(NSString *)serverParameter
label:(NSString *)serverLabel
request:(GADCustomEventRequest *)customEventRequest {
// Set the publisher ID for the interstitial. The ID comes from the server
// parameter you provide when creating the custom event.
self.interstitial = [[GADInterstitial alloc] initWithAdUnitID:serverParameter];
// Set the delegate to listen for callbacks.
self.interstitial.delegate = self;
// Create an ad request using custom targeting options from the custom event
// request.
GADRequest *request = [GADRequest request];
[request setAdditionalParameters:[customEventRequest additionalParameters]];
[request setBirthday:[customEventRequest userBirthday]];
[request setGender:[customEventRequest userGender]];
[request setTesting:[customEventRequest isTesting]];
if ([customEventRequest userHasLocation]){
[request setLocationWithLatitude:[customEventRequest userLatitude]
longitude:[customEventRequest userLongitude]
accuracy:[customEventRequest userLocationAccuracyInMeters]];
}
[self.interstitial loadRequest:request];
}
- (void)presentFromRootViewController:(UIViewController *)rootViewController {
[self.interstitial presentFromRootViewController:rootViewController];
}
插页式广告自定义事件接口要求您实现presentFromRootViewController:方法。当您告知移动广告SDK 展示插页式广告时,中介层会调用此方法。
七:通知 KeyMob中介
与横幅广告自定义事件示例一样,您需要为广告网络实现广告监听器以将消息转发回中介。以下示例介绍了 KeyMob的GADInterstitialDelegate的实现方法。
#pragma mark GADInterstitial Callbacks
- (void)interstitialDidReceiveAd:(GADInterstitial *)view {
[self.delegate customEventInterstitial:self didReceiveAd:view];
}
- (void) interstitial:(GADInterstitial *)ad
didFailToReceiveAdWithError:(GADRequestError *)error {
[self.delegate customEventInterstitial:self didFailAd:error];
}
- (void)interstitialWillPresentScreen:(GADInterstitial *)ad {
[self.delegate customEventInterstitialWillPresent:self];
}
- (void)interstitialWillDismissScreen:(GADInterstitial *)ad {
[self.delegate customEventInterstitialWillDismiss:self];
}
- (void)interstitialDidDismissScreen:(GADInterstitial *)ad {
[self.delegate customEventInterstitialDidDismiss:self];
}
- (void)interstitialWillLeaveApplication:(GADInterstitial *)ad {
[self.delegate customEventInterstitialWillLeaveApplication:self];
}
@end
将消息发回中介层可让其继续中介广告瀑布流。插页式广告没有点击次数,因此不会有插页式广告点击事件传递回中介层。
这里有一些应用能添加广告教程,如android 应用、IOS应用、 flash air应用、 cordova5应用 ,详细教程网站http://www.keymob.com这是一个专业应用广告管理工具——KeyMob支持插页式广告、横幅广告、Banner、视频广告等众多流行广告平台。打开教程你将会学习到更多,希望能帮到忙!
来源:oschina
链接:https://my.oschina.net/u/2505907/blog/537487