ios下应用程序检测admob广告请求返回和点击事件

      因应用需求,需要做一个检测用户点击admob的功能,google上搜索了一圈,没有找到想要的答案,最终只能自己动手,查阅了admob的ios SDK中的几个头文件后,找到了相关的方法,具体如下:
      应用中使用的是admob的广告条,即GADBannerView,如要检测GADBannerView相关的事件,如接收广告完成,用户点击广告,用户切回应用等事件,需要实现GADBannerViewDelegate,主要包括的方法如下:
// Sent when an ad request loaded an ad.  This is a good opportunity to add this
// view to the hierarchy if it has not yet been added.  If the ad was received
// as a part of the server-side auto refreshing, you can examine the
// hasAutoRefreshed property of the view.
- (void)adViewDidReceiveAd:(GADBannerView *)view;

// Sent when an ad request failed.  Normally this is because no network
// connection was available or no ads were available (i.e. no fill).  If the
// error was received as a part of the server-side auto refreshing, you can
// examine the hasAutoRefreshed property of the view.
- (void)adView:(GADBannerView *)view
    didFailToReceiveAdWithError:(GADRequestError *)error;

#pragma mark Click-Time Lifecycle Notifications

// Sent just before presenting the user a full screen view, such as a browser,
// in response to clicking on an ad.  Use this opportunity to stop animations,
// time sensitive interactions, etc.
//
// Normally the user looks at the ad, dismisses it, and control returns to your
// application by calling adViewDidDismissScreen:.  However if the user hits the
// Home button or clicks on an App Store link your application will end.  On iOS
// 4.0+ the next method called will be applicationWillResignActive: of your
// UIViewController (UIApplicationWillResignActiveNotification).  Immediately
// after that adViewWillLeaveApplication: is called.
- (void)adViewWillPresentScreen:(GADBannerView *)adView;

// Sent just before dismissing a full screen view.
- (void)adViewWillDismissScreen:(GADBannerView *)adView;

// Sent just after dismissing a full screen view.  Use this opportunity to
// restart anything you may have stopped as part of adViewWillPresentScreen:.
- (void)adViewDidDismissScreen:(GADBannerView *)adView;

// Sent just before the application will background or terminate because the
// user clicked on an ad that will launch another application (such as the App
// Store).  The normal UIApplicationDelegate methods, like
// applicationDidEnterBackground:, will be called immediately before this.
- (void)adViewWillLeaveApplication:(GADBannerView *)adView;

参阅其中的注释可知,常用的方法为:

//客户端接收到广告后调用
- (void)adViewDidReceiveAd:(GADBannerView *)view
{
}
//用户点击广告后调用
- (void)adViewWillPresentScreen:(GADBannerView *)adView
{
    NSLog(@"用户点击");
   
}
//用户点击广告后切换回游戏时
- (void)adViewDidDismissScreen:(GADBannerView *)adView

如果当前存在多个广告栏,可通过检测adView参数的值获取当前产生事件的广告栏对象,实现对应功能。
具体做法是,编写一个类,实现GADBannerViewDelegate,然后在需要检测事件的GADBannerView上设置对应的代理,代码为:
adView.delegate=自定义的实现了GADBannerViewDelegate协议的类对象;

-------------------------------------分割线--------------------------
2012年11月2日 补充
google的admob SDK在不同版本的ios下,点击广告条,所产生的事件是不同的,在ios6.0的ipad中,用户点击广告后调用
- (void)adViewWillPresentScreen:(GADBannerView *)adView
用户切回游戏中时,调用
- (void)adViewDidDismissScreen:(GADBannerView *)adView
但在ios5的iphone中,用户点击广告后调用的却是:
- (void)adViewWillLeaveApplication:(GADBannerView *)adView
用户切回游戏时无事件

你可能感兴趣的:(admob)