插屏广告属于全屏广告,它会覆盖整个应用界面,直到用户将其关闭。这些广告通常会在应用流程的自然过渡点进行展示。当展示插屏广告时,用户可以点击广告前往目标页面,也可以关闭广告回到主页面。
下面将介绍如何将插屏广告植入iOS应用。
前提条件
务必使用测试广告进行测试
在构建和测试应用时,请确保使用的是测试广告,而不是实际投放的广告。否则,可能会导致您的账号被暂停使用。对于 iOS 插屏广告,使用下面的测试专用广告单元 ID:
该测试广告单元 ID 已经过专门配置,可确保每个请求返回的都是测试广告。您可以在自己应用的编码、测试和调试过程中随意使用该测试广告单元 ID。需要注意的一点是,请务必在发布应用前用您的广告单元 ID 替换该测试广告单元 ID。
接入步骤
主要步骤如下:
请求广告
请求插屏广告通过 [Adx3InterstitialAd sharedManager] 中 loadAdWithPlacementID: 方法完成,loadAdWithPlacementID: 方法需要您的广告单元 ID,userId 可不传或者传入您的应用为用户设置的 userId,最后通过回调 completionHandler 告知您请求的结果,以下为代码示例:
import UIKit
class ViewController: UIViewController, Adx3InterstitialDeleagte {
override func viewDidLoad() {
super.viewDidLoad()
loadInterstitialAd()
// Do any additional setup after loading the view.
}
func loadInterstitialAd() {
Adx3InterstitialAd.sharedManager().load(withPlacementID: "Your Placement ID", userId: "", delegate: self) { isSuccess in
}
}
}
#import "Adx3Framework/Adx3Ad.h"
@interface ViewController()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self loadInterstitialAd];
}
- (void)loadInterstitialAd {
[[Adx3InterstitialAd sharedManager] loadAdWithPlacementID:@"Your Placement ID" userId:@"" delegate:self completionHandler:^(BOOL isReady) {
}];
}
@end
注册回调
要监听请求结果的回调 ,您必须实现 Adx3AdLoadingDelegate
要监听展示插屏事件的相关方法,您必须实现 Adx3InterstitialDeleagte,由于 Adx3InterstitialDeleagte 继承自 Adx3AdLoadingDelegate,所以您只需继承 Adx3InterstitialDeleagte 即可。代码示例:
import UIKit
class ViewController: UIViewController, Adx3InterstitialDeleagte {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
extension ViewController {
func didFinishLoadingAD(withPlacementID placementID: String!) {
print("----Interstitial AD Load Finish----")
}
func didFailToLoadAD(withPlacementID placementID: String!, error: Error!) {
print("----\(error.debugDescription)----")
}
func interstitialDidShow(forPlacementID placementID: String!, extra: [AnyHashable : Any]!) {
}
func interstitialDidClick(forPlacementID placementID: String!, extra: [AnyHashable : Any]!) {
}
func interstitialDidClose(forPlacementID placementID: String!, extra: [AnyHashable : Any]!) {
}
func interstitialDidEndPlayingVideo(forPlacementID placementID: String!, extra: [AnyHashable : Any]!) {
}
func interstitialDidStartPlayingVideo(forPlacementID placementID: String!, extra: [AnyHashable : Any]!) {
}
func interstitialFailedToShow(forPlacementID placementID: String!, error: Error!, extra: [AnyHashable : Any]!) {
}
func interstitialDidFailToPlayVideo(forPlacementID placementID: String!, error: Error!, extra: [AnyHashable : Any]!) {
}
}
#import "Adx3Framework/Adx3Ad.h"
@interface ViewController() <Adx3InterstitialDeleagte>
@end
@implementation ViewController
@end
#pragma mark - Adx3AdLoadingDelegate
- (void)didFinishLoadingADWithPlacementID:(NSString *)placementID {
NSLog(@"---Interstitial AD Load Finish----");
}
- (void)didFailToLoadADWithPlacementID:(NSString *)placementID error:(NSError *)error {
NSLog(@"----%@----", error.description);
}
#pragma mark - Adx3InterstitialDeleagte
-(void)interstitialDidShowForPlacementID:(NSString*)placementID extra:(NSDictionary*)extra {
}
-(void)interstitialFailedToShowForPlacementID:(NSString*)placementID error:(NSError*)error extra:(NSDictionary*)extra {
}
-(void)interstitialDidStartPlayingVideoForPlacementID:(NSString*)placementID extra:(NSDictionary*)extra {
}
-(void)interstitialDidEndPlayingVideoForPlacementID:(NSString*)placementID extra:(NSDictionary*)extra {
}
-(void)interstitialDidFailToPlayVideoForPlacementID:(NSString*)placementID error:(NSError*)error extra:(NSDictionary*)extra {
}
-(void)interstitialDidCloseForPlacementID:(NSString*)placementID extra:(NSDictionary*)extra {
}
-(void)interstitialDidClickForPlacementID:(NSString*)placementID extra:(NSDictionary*)extra {
}
展示广告
插屏广告应在应用流程的合适时机进行展示,代码示例:
func showInterstitialAd() {
Adx3InterstitialAd.sharedManager().showInterstitial(in: self, delegate: self)
}
- (void)showInterstitialAd {
[[Adx3InterstitialAd sharedManager] showInterstitialInViewController:self delegate:self];
}