admob 广告代码参考 iOS 弹窗广告

reate an interstitial ad object

Interstitial ads are requested and shown by GADInterstitial objects. The first step in using one is to instantiate it and set its ad unit ID. For example, here's how to create a GADInterstitial in the viewDidLoad method of a UIViewController:

SWIFT

OBJECTIVE-C

  
  
@import GoogleMobileAds;
@import UIKit;

@interface ViewController ()

@property(nonatomic, strong) GADInterstitial*interstitial;

@end

@implementation ViewController

- (void)viewDidLoad {
 
[super viewDidLoad];

 
self.interstitial = [[GADInterstitial alloc]
      initWithAdUnitID
:@"ca-app-pub-3940256099942544/4411468910"];

}

GADInterstitial is a single-use object that will load and display one interstitial ad. To display multiple interstitial ads, an app needs to create a GADInterstitial for each one.

Always test with test ads

When building and testing your apps, make sure you use test ads rather than live, production ads. Failure to do so can lead to suspension of your account.

The easiest way to load test ads is to use our dedicated test ad unit ID for iOS interstitials: ca-app-pub-3940256099942544/4411468910

It's been specially configured to return test ads for every request, and you're free to use it in your own apps while coding, testing, and debugging. Just make sure you replace it with your own ad unit ID before publishing your app.

For more information about how the Mobile Ads SDK's test ads work, see Test Ads.

Load an ad

To load an interstitial ad, call loadRequest: on a GADRequest object:

SWIFT

OBJECTIVE-C

  
  
@import GoogleMobileAds;
@import UIKit;

@interface ViewController ()

@property(nonatomic, strong) GADInterstitial *interstitial;

@end

@implementation ViewController

- (void)viewDidLoad {
 
[super viewDidLoad];
 
self.interstitial = [[GADInterstitial alloc]
      initWithAdUnitID
:@"ca-app-pub-3940256099942544/4411468910"];
 
GADRequest *request = [GADRequest request];
 
[self.interstitial loadRequest:request];

}

Show the ad

Interstitials should be displayed during natural pauses in the flow of an app. Between levels of a game is a good example, or after the user completes a task. To show an interstitial, check the isReadyproperty on GADInterstitial to verify that it's done loading, then callpresentFromRootViewController. Here's an example of how to do this in one of the action methods in a UIViewController:

SWIFT

OBJECTIVE-C

  
  
- (IBAction)doSomething:(id)sender {
 
...
 
if (self.interstitial.isReady) {
   
[self.interstitial presentFromRootViewController:self];
 
}
else {
   
NSLog(@"Ad wasn't ready");
 
}
}

The message "Cannot present interstitial. It is not ready" indicates that the interstitial is still loading or has failed to load. To avoid this warning, use the isReady method to check if the interstitial ad is ready to be presented prior to calling presentFromRootViewController:.

Use GADInterstitialDelegate to reload

GADInterstitial is a one-time-use object. This means once an interstitial is shown, hasBeenUsedreturns true and the interstitial can't be used to load another ad. To request another interstitial, you'll need to create a new GADInterstitial object. If you try to re-use an interstitial object, you'll get the error "Request Error: Will not send request because interstitial object has been used".

The best place to allocate another interstitial is in the interstitialDidDismissScreen method on GADInterstitialDelegate so that the next interstitial starts loading as soon as the previous one is dismissed. You may even consider breaking out interstitial initialization into its own helper method:

SWIFT

OBJECTIVE-C

  
  
- (void)viewDidLoad {
 
[super viewDidLoad];
 
self.interstitial = [self createAndLoadInterstitial];
}

- (GADInterstitial *)createAndLoadInterstitial {
 
GADInterstitial *interstitial =
     
[[GADInterstitial alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910"];
  interstitial
.delegate = self;
 
[interstitial loadRequest:[GADRequest request]];
 
return interstitial;
}

- (void)interstitialDidDismissScreen:(GADInterstitial *)interstitial {
 
self.interstitial = [self createAndLoadInterstitial];
}

By preloading another interstitial immediately after the previous one is dismissed, your app is prepared to show an interstitial again at the next logical break point.

Ad events

Through the use of GADInterstitialDelegate, you can listen for lifecycle events, such as when an ad is closed or the user leaves the app.

Registering for interstitial events

To register for interstitial ad events, set the delegate property on GADInterstitial to an object that implements the GADInterstitialDelegate protocol. Generally, the class that implements interstitial ads also acts as the delegate class, in which case the delegate property can be set to self as follows:

SWIFT

OBJECTIVE-C

  
  
#import "GADInterstitial.h"
#import "GADInterstitialDelegate.h"

@interface ViewController () <GADInterstitialDelegate>

@property(nonatomic, strong) GADInterstitial *interstitial;

@end

@implementation ViewController

- (void)viewDidLoad {
 
[super viewDidLoad];
 
self.interstitial = [[GADInterstitial alloc] initWithAdUnitID:"ca-app-pub-3940256099942544/4411468910"];
 
self.interstitial.delegate = self;
}

Implementing interstitial events

Each of the methods in GADInterstitialDelegate are marked as optional, so you only need to implement the methods you want. This example implements each method and logs a message to the console:

SWIFT

OBJECTIVE-C

  
  
/// Tells the delegate an ad request succeeded.
- (void)interstitialDidReceiveAd:(GADInterstitial *)ad {
 
NSLog(@"interstitialDidReceiveAd");
}

/// Tells the delegate an ad request failed.
- (void)interstitial:(GADInterstitial *)ad
    didFailToReceiveAdWithError
:(GADRequestError *)error {
 
NSLog(@"interstitial:didFailToReceiveAdWithError: %@", [error localizedDescription]);
}

/// Tells the delegate that an interstitial will be presented.
- (void)interstitialWillPresentScreen:(GADInterstitial *)ad {
 
NSLog(@"interstitialWillPresentScreen");
}

/// Tells the delegate the interstitial is to be animated off the screen.
- (void)interstitialWillDismissScreen:(GADInterstitial *)ad {
 
NSLog(@"interstitialWillDismissScreen");
}

/// Tells the delegate the interstitial had been animated off the screen.
- (void)interstitialDidDismissScreen:(GADInterstitial *)ad {
 
NSLog(@"interstitialDidDismissScreen");
}

/// Tells the delegate that a user click will open another app
/// (such as the App Store), backgrounding the current app.
- (void)interstitialWillLeaveApplication:(GADInterstitial *)ad {
 
NSLog(@"interstitialWillLeaveApplication");
}

Some best practices

Consider whether interstitial ads are the right type of ad for your app.
Interstitial ads work best in apps with natural transition points. The conclusion of a task within an app, like sharing an image or completing a game level, creates such a point. Because the user is expecting a break in the action, it's easy to present an interstitial ad without disrupting their experience. Make sure you consider at which points in your app's workflow you'll display interstitial ads and how the user is likely to respond.
Remember to pause the action when displaying an interstitial ad.
There are a number of different types of interstitial ads: text, image, video, and more. It's important to make sure that when your app displays an interstitial ad, it also suspends its use of some resources to allow the ad to take advantage of them. For example, when you make the call to display an interstitial ad, be sure to pause any audio output being produced by your app. You can resume playing sounds in the  interstitialDidDismissScreen event handler, which will be invoked when the user has finished interacting with the ad. In addition, consider temporarily halting any intense computation tasks (such as a game loop) while the ad is being displayed. This will ensure that the user doesn't experience slow or unresponsive graphics or stuttered video.
Allow for adequate loading time.
Just as it's important to make sure you display interstitial ads at an appropriate time, it's also important to make sure the user doesn't have to wait for them to load. Loading the ad in advance by calling  loadRequest before you intend to call  presentFromRootViewController can ensure that your app has a fully loaded interstitial ad at the ready when the time comes to display one.
Don't flood the user with ads.
While increasing the frequency of interstitial ads in your app might seem like a great way to increase revenue, it can also degrade the user experience and lower clickthrough rates. Make sure that users aren't so frequently interrupted that they're no longer able to enjoy the use of your app.
Don't use the  interstitialDidReceiveAd event to show the interstitial.
This can cause a poor user experience. Instead, pre-load the ad before you need to show it. Then check the  isReady method on  GADInterstitial to find out if it is ready to be shown.

Additional resources

Samples on GitHub

Mobile Ads Garage video tutorials

Next steps

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值