AdMob插页广告与苹果iAd插页广告比较相似,都是全屏显示,它的应用场景与iAd稍有不同。在应用启动、 视频前贴片或游戏关卡加载时显示广告,我们把这种场景称为“启动场景”,这与iAd的“内容显示场景”类似。还有一种是在视频播放结束或游戏结束时显示的,我们称之为“结束场景”。
1. 启动场景
应用启动、视频前贴片或游戏关卡加载时,会弹出模态全屏广告对话框,点击全屏广告左上角的关闭按钮, 可以关闭该对话框,如图1所示。没有广告时,直接进入②界面(主屏幕界面),如果有广告填充时先启动① (广告界面)进入,我们需要关闭它才能回到②界面(主屏幕界面)。
启动场景的AdMob插页广告
#import "GADInterstitial.h"
案例的主要代码是在ViewController中编写的,具体如下:
import UIKit
class ViewController: UIViewController,GADInterstitialDelegate { ①
var splashInterstitial: GADInterstitial! ②
let AdUnitID = "ca-app-pub-1990684556219793/1962464393"
override func viewDidLoad() {
super.viewDidLoad()
self.splashInterstitial = GADInterstitial() ③
self.splashInterstitial.adUnitID = AdUnitID
self.splashInterstitial.delegate = selfself.splashInterstitial.loadRequest(self.createRequest()) ④
}
func createRequest() -> GADRequest {
var request: GADRequest = GADRequest()
request.testDevices = NSArray(array: ["7740674c81cf31a50d2f92bcdb729f10",
GAD_SIMULATOR_ID])
return request
}
//MARK: --GADInterstitialDelegate实现
func interstitialDidReceiveAd(ad: GADInterstitial!) { ⑤
NSLog("广告加载成功")
if self.splashInterstitial.isReady { ⑥
self.splashInterstitial.presentFromRootViewController(self) ⑦
}}
func interstitial(ad: GADInterstitial!,
didFailToReceiveAdWithError error: GADRequestError!) { ⑧
NSLog("广告加载失败")
}
}
#import "ViewController.h"
#import "GADInterstitial.h"
#define AdUnitID @"ca-app-pub-1990684556219793/1962464393"
@interface ViewController ()
@property(nonatomic, strong) GADInterstitial *splashInterstitial; ②
- (GADRequest *)createRequest;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.splashInterstitial = [[GADInterstitial alloc] init]; ③
self.splashInterstitial.adUnitID = AdUnitID;
self.splashInterstitial.delegate = self;
[self.splashInterstitial loadRequest:[self createRequest]]; ④
}
//创建广告请求
- (GADRequest *)createRequest {
GADRequest *request = [GADRequest request];
//设置测试设备,防止测试阶段的无效请求
request.testDevices = [NSArray arrayWithObjects:
@"7740674c81cf31a50d2f92bcdb729f10",GAD_SIMULATOR_ID, nil];
return request;
}
#pragma mark GADInterstitialDelegate实现
- (void)interstitialDidReceiveAd:(GADInterstitial *)ad { ⑤
NSLog(@"广告加载成功");
if (self.splashInterstitial.isReady) { ⑥
[self.splashInterstitial presentFromRootViewController:self]; ⑦
}
}
- (void)interstitial:(GADInterstitial *)interstitial
didFailToReceiveAdWithError:(GADRequestError *)error { ⑧
NSLog(@"广告接收失败 %@", [error localizedDescription]);
}
@end
上述代码中,第①行说明定义类时需要声明遵守GADInterstitialDelegate委托协议,该协议规定了 GADInterstitial生命周期事件。第②行代码是定义GADInterstitialAd类型的属性splashInterstitial。 GADInterstitialAd是AdMob插页广告视图对象。第③行代码用于创建并初始化插页广告视图对象。第④行代码 用于请求广告,其中self.createRequest()语句(Objective-C是self createRequest)用于获得请求对象GADRequest。
第⑤行和第⑧行是GADInterstitialDelegate委托协议方法,其中第⑤行的方法是成功加载广告的方法,第⑥ 行用于判断广告是否已经加载完成,第⑦行用于呈现广告界面,其中self是当前视图控制器,它也是呈现广告界面的视图控制器。
2. 结束场景
该场景是在视频播放结束或游戏结束时显示广告,它需要有一个触发条件,满足该条件时才弹出模态全屏广 告对话框,如图2所示。
结束场景的AdMob插页广告
#import "GADInterstitial.h"
案例的主要代码是在ViewController中编写的。ViewController类定义、属性等的相关代码如下:
import UIKit class ViewController: UIViewController, GADInterstitialDelegate {
//用于模拟控制游戏进度
var timer: NSTimer!
let AdUnitID = "ca-app-pub-1990684556219793/1962464393"
//插页广告GADInterstitial对象属性
var interstitial: GADInterstitial!
//进度条
@IBOutlet weak var progressView: UIProgressView!
//界面中的按钮
@IBOutlet weak var startButton: UIButton!
......
}
#import "ViewController.h"
#import "GADInterstitial.h"
#define AdUnitID @"ca-app-pub-1990684556219793/1962464393"
@interface ViewController ()
{
NSTimer *timer; //用于模拟控制游戏进度
}
//插页广告GADInterstitial对象属性
@property(nonatomic, strong) GADInterstitial *splashInterstitial;
//进度条
@property (weak, nonatomic) IBOutlet UIProgressView *progressView; /
/插页广告GADInterstitial对象属性
@property(nonatomic, strong) GADInterstitial *interstitial;
//界面中的按钮
@property (weak, nonatomic) IBOutlet UIButton *startButton;
//创建广告请求对象 - (GADRequest *)createRequest; //更新进度条 -(void)update;
//按钮事件
- (IBAction)start:(id)sender;
@end
下面我们再看看ViewController中按钮事件的onClick:方法和更新进度条的update方法的代码:
@IBAction func onClick(sender: AnyObject) {
self.startButton.enabled = false
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self,
selector: "update",
userInfo: nil,
repeats: true) ①
}
func update() {
self.progressView.progress += 0.1
if (self.progressView.progress == 1.0) { ②
//游戏结束
NSLog("游戏结束")
timer.invalidate() ③
timer = nil
//初始化广告
self.interstitial = GADInterstitial() ④
self.interstitial.adUnitID = AdUnitID ⑤
self.interstitial.delegate = self ⑥
self.interstitial.loadRequest(self.createRequest()) ⑦
}
}
- (IBAction)start:(id)sender {
self.startButton.enabled = NO;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(update)
userInfo:nil
repeats:YES]; ①
}
-(void)update
{
self.progressView.progress += 0.1; if (self.progressView.progress == 1.0){ ②
//游戏结束
NSLog(@"游戏结束");
[timer invalidate]; ③
timer = nil;
//初始化广告
self.interstitial = [[GADInterstitial alloc] init]; ④
self.interstitial.adUnitID = AdUnitID; ⑤
self.interstitial.delegate = self; ⑥
[self.interstitial loadRequest:[self createRequest]]; ⑦
}
}
在onClick:方法中,第①行代码通过NSTimer开始计划任务,该计划任务是每隔0.1秒调用一次update方法。在 update方法中,第②行代码用于判断游戏是否结束(当然这是模拟),NSTimer使用完,就需要使用第③行代码停止计划执行。第④行代码用于实例化GADInterstitial对象。第⑦行代码通过调用createRequest方法获得请求对象发出广告请求。
createRequest方法的代码如下:
func createRequest() -> GADRequest {
var request: GADRequest = GADRequest()
request.testDevices = NSArray(array: ["7740674c81cf31a50d2f92bcdb729f10",
GAD_SIMULATOR_ID])
return request
}
- (GADRequest *)createRequest {
GADRequest *request = [GADRequest request];
//设置测试设备,防止测试阶段的无效请求
request.testDevices = [NSArray arrayWithObjects:
@"7740674c81cf31a50d2f92bcdb729f10",
GAD_SIMULATOR_ID, nil];
return request;
}
下面我们再看看ViewController中有关GADInterstitialDelegate委托的实现代码:
//MARK: --GADInterstitialDelegate实现
func interstitialDidReceiveAd(ad: GADInterstitial!) {
NSLog("广告加载成功")
if self.interstitial.isReady {
self.interstitial.presentFromRootViewController(self) ①
self.startButton.enabled = true
self.progressView.progress = 0.0
}
}
func interstitial(ad: GADInterstitial!, didFailToReceiveAdWithError error:
GADRequestError!) {
NSLog("广告加载失败")
}
- (void)interstitialDidReceiveAd:(GADInterstitial *)interstitial {
NSLog(@"广告接收成功");
[self.interstitial presentFromRootViewController:self]; ①
self.startButton.enabled = YES;
self.progressView.progress = 0.0;
}
- (void)interstitial:(GADInterstitial *)interstitial
didFailToReceiveAdWithError:(GADRequestError *)error {
NSLog(@"广告接收失败 %@", [error localizedDescription]);
}
在接收成功的interstitialDidReceiveAd:方法中,需要使用第①行代码模态呈现广告对话框,GADInterstitial 对象的presentFromRootViewController:方法需要在成功请求回来后再调用。最后运行一下,看看是否能呈现广告。
以上便是南昌APP开发公司-百恒网络为大家介绍的关于IOS软件开发中添加AdMob插页广告的方法,如果还有哪些不太明白的地方,可随时来电和我们联系。此外,想了解更多关于网站建设、微信开发、电商购物网站开发等方面相关知识,欢迎访问百恒网络网站!