`
bsr1983
  • 浏览: 1100370 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

【转】Google AdMob 广告 iOS 高级指南

 
阅读更多
转自:[url]https://developers.google.com/mobile-ads-sdk/docs/ios/advanced?hl=zh-cn
[/url]
转载仅为防止被墙致无法查阅!
非页内广告概览

当用户轻触横幅广告这类小型广告时,通常会被引导至某种形式的应用内全屏浏览方式。

与之相对,非页内广告可以在应用的自然转换点(例如启动、视频前贴片或游戏关卡加载时)即刻呈现 HTML5 富媒体效果或“网络应用”。网络应用提供了应用内浏览体验,这类应用的特点是只有简单的关闭按钮,而没有任何导航栏:其内容有自己的内部导航架构。

从本质上说,这类广告效果更丰富、更吸引人,因此价格更昂贵,且展示机会有局限。

 
注意:所有发布商都可以在自己的应用中投放富媒体非页内自家广告,而付费非页内广告只向一部分发布商提供。如果您具备了发布资格,Google 一定会与您联系。

GADInterstitial

GADInterstitial 功能更丰富、也更重要,这反映在它的定义上:它不是 UIView,而是需要更明确的实例化、加载和显示步骤的 NSObject。

不过,它的用法与 GADBannerView 非常类似:

导入 GADInterstitial.h
在应用的 UIViewController 中声明 GADInterstitial 实例
加以创建
设置广告的单元 ID,也就是您的 AdMob 发布商 ID
同样,您最好在应用的 UIViewController 中执行上述步骤。

// InterstitialExampleViewController.h

// 从 SDK 导入 GADInterstitial 的定义
#import "GADInterstitial.h"

@interface InterstitialExampleViewController : UIViewController {
// 将其中一个声明为实例变量
GADInterstitial *interstitial_;
}

@end
以下代码会在视图控制器的 viewDidLoad 初始化挂钩 (Hook) 中设置非页内广告。

// InterstitialExampleViewController.m

#import "InterstitialExampleViewController.h"

@implementation MyInterstitialHostingViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  interstitial_ = [[GADInterstitial alloc] init];
  interstitial_.adUnitID = MY_INTERSTITIAL_UNIT_ID;
}

@end
您随时可以调用 loadRequest:,但只有在调用 GADInterstitialDelegate 的 interstitialDidReceiveAd: 后才能展示广告。如果收到 GADInterstitialDelegate 的 interstitial:didFailToReceiveAdWithError: 错误,请妥善处理错误环境。

非页内广告请求的默认超时值是 5 秒,但您可以在服务器的 AdMob 帐户内进行调整。

一旦加载成功,即可展示全屏广告:

[interstitial_ presentFromRootViewController:self];
接着,非页内广告会占据整个屏幕,直到用户将其关闭;届时,控制权才会交还给应用,而视图控制器则会传递给这个方法。

初始网页非页内广告

非页内广告有一种特殊用法,就是在应用启动时以“初始”网页的方式出现。

在应用委托的 application:didFinishLaunchingWithOptions: 内调用 loadAndDisplayRequest:usingWindow:initialImage:,可有效地将非页内广告排入队列,以便在其 isReady 时加以展示。屏幕上会继续显示初始图片(通常是应用的默认图片),直到加载请求成功或失败为止。

- (BOOL)application:(UIApplication *)application
  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  GADInterstitial *splashInterstitial_ = [[GADInterstitial alloc] init];

  [splashInterstitial_ loadAndDisplayRequest:[GADRequest request]
                                 usingWindow:window_
                                initialImage:[UIImage imageNamed:@"Default.png"]];
}
GADInterstitialDelegate

就像使用 GADBannerViewDelegate 时一样,开发人员可以选择实施全部或部分的 GADInterstitialDelegate 来跟踪非页内广告的生命周期事件。

@protocol GADInterstitialDelegate
  @optional
    - (void)interstitialDidReceiveAd:(GADInterstitial *)interstitial;
    - (void)interstitial:(GADInterstitial *)interstitial
        didFailToReceiveAdWithError:(GADRequestError *)error;
    - (void)interstitialWillPresentScreen:(GADInterstitial *)interstitial;
    - (void)interstitialWillDismissScreen:(GADInterstitial *)interstitial;
    - (void)interstitialDidDismissScreen:(GADInterstitial *)interstitial;
    - (void)interstitialWillLeaveApplication:(GADInterstitial *)interstitial;
@end
- (void)interstitialDidReceiveAd:(GADInterstitial *)interstitial
在以下情况下发送:loadRequest: 已获得成功,且非页内广告处于 isReady 状态,随时可以适时展示。
- (void)interstitial:(GADInterstitial *)interstitial didFailToReceiveAdWithError:(GADRequestError *)error
当 loadRequest: 失败时发送。常见失败原因:网络故障、应用配置错误,或非页内广告资源不足(比在横幅广告中更常见)。您可以记下这些事件,以便进行调试。
- (void)interstitialWillPresentScreen:(GADInterstitial *)interstitial
发送后便立即向用户展示 interstitial。与使用 UIApplicationDidEnterBackgroundNotification 时类似,此时应该暂停所有动画、计时器或其他可能会与用户互动的活动,并保存应用状态。请注意,用户可能会在非页内广告内按下“首页”按钮或触摸到其他应用(例如 App Store 或 iTunes)的链接,并因此离开您的应用。
- (void)interstitialDidDismissScreen:(GADInterstitial *)interstitial
当用户关闭 interstitial 且广告已从屏幕中退出时发送。
- (void)interstitialWillDismissScreen:(GADInterstitial *)interstitial
用以恢复您的应用和根视图控制器(在非初始网页的情况下),在此语句发送后,interstitial 便会紧跟着从屏幕中退出。这时应该重新启动 interstitialWillPresentScreen: 执行期间暂停的任何前台活动。
- (void)interstitialWillLeaveApplication:(GADInterstitial *)interstitial
在应用因为用户触摸 interstitial 内指向其他应用的链接而转至后台或终止运行前发送。在此之前,先会有 applicationDidEnterBackground: 等常规 UIApplicationDelegate 通知显示。
在这些方法中,您可以通过查看 GADBannerView.hasAutoRefreshed 来判断是否有刷新操作触发了事件。

同样,如果您将委托作为独特的对象来实施,则在发布非页内广告前,请务必将委托设为 nil。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics