在集成改文档之前,请务必确保您已经集成了 basic integration 文档。
这种广告格式不需要全屏显示;而是由发布商确定广告容器在其应用中的位置。 横幅容器的尺寸必须为320x50、300x50,300x250 (for MREC)或728x90(对于平板电脑)。 您可以在屏幕上的任何位置设置横幅广告,并且用户可以在播放广告的同时继续使用该应用。
创建Banner Ad实例
- 请先代开您的工程,在
Main.storyboard
,添加UIView
到您的ViewController
中并命名为bannerAdContainer
.
- 请先导入
VungleAdsSDK
并创建一个VungleInterstitial
变量在您的ViewController.swift
文件中 (或者 Objective-c的ViewController.h
文件)。并创建变量VungleBanner
.
import UIKit import VungleAdsSDK class LOBannerViewController: UIViewController { @IBOutlet weak var bannerAdView: UIView! private var bannerAd: VungleBanner?
#import "LOBannerViewController.h" #import <VungleAdsSDK/VungleAdsSDK.h> @interface LOBannerViewController () <VungleBannerDelegate> @property (nonatomic, weak) IBOutlet UIView *bannerAdView; @property (nonatomic, strong) VungleBanner *bannerAd; @end
- 添加以下代码来创建
VungleBanner
创建。请使用您的Placement ID和banner size来创建VungleBanner
实例。 Banner size请查看 此处。
请注意MREC广告类型,请在Dashboard中创建相应的Placement,而不能使用banner类型的Placement ID。
self.bannerAd = VungleBanner(placementId: <YOUR_PLACEMENT_ID>, size: BannerSize.regular) self.bannerAd?.delegate = self
self.bannerAd = [[VungleBanner alloc] initWithPlacementId:<YOUR_PLACEMENT_ID> size:BannerSizeRegular]; self.bannerAd.delegate = self;
Note: You must set your ViewController
(or any class that declares conformance to VungleBannerDelegate
) to the delegate property of the VungleBanner
instance (refer to the Register for Callbacks section below).
Supported Banner Sizes
Vungle supports the banner sizes in the table below. Specify the banner size when you create the VungleBanner
instance by using one of the available enum values.
Banner Format | BannerSize enum | Dimension |
---|---|---|
Regular | .regular |
320 x 50 |
Short | .short |
300 x 50 |
MREC | .mrec |
300 x 250 |
Leaderboard (tablet only) | .leaderboard |
728 x 90 |
下载Banner Ad
- 请参考以下代码来下载广告
self.bannerAd = VungleBanner(placementId: <YOUR_PLACEMENT_ID>, size: BannerSize.regular) self.bannerAd?.delegate = self self.bannerAd?.load()
self.bannerAd = [[VungleBanner alloc] initWithPlacementId:<YOUR_PLACEMENT_ID> size:BannerSizeRegular]; self.bannerAd.delegate = self; [self.bannerAd load:nil];
播放Banner Ad
- 当广告加载好后,您可以调用
present(on: <YOUR_UIView>)
来展示广告self.bannerAd?.present(on: <YOUR_UIView>)
[self.bannerAd presentOn: <YOUR_UIView>];
关闭Banner Ad
当banner播放完毕后,我们建议您清理 VungleBanner
实例。您可以通过将其移除父View。在以下代码示例中, bannerAdView
是superView
的 VungleBanner
实例:
for subView in self.bannerAdView.subviews { subView.removeFromSuperview() } self.bannerAd?.delegate = nil self.bannerAd = nil
for (id subview in self.bannerAdView.subviews) { [subview removeFromSuperview]; } self.bannerAd.delegate = nil; self.bannerAd = nil;
注册Delegate
通过delegate的各个方法来监听各个事件
extension LOBannerViewController: VungleBannerDelegate { // Ad load Events func bannerAdDidLoad(_ banner: VungleBanner) { print("bannerAdDidLoad") } func bannerAdDidFailToLoad(_ banner: VungleBanner, withError: NSError) { print("bannerAdDidFailToLoad") } // Ad Lifecycle Events func bannerAdWillPresent(_ banner: VungleBanner) { print("bannerAdWillPresent") } func bannerAdDidPresent(_ banner: VungleBanner) { print("bannerAdDidPresent") } func bannerAdDidFailToPresent(_ banner: VungleBanner, withError: NSError) { print("bannerAdDidFailToPresent") } func bannerAdDidTrackImpression(_ banner: VungleBanner) { print("bannerAdDidTrackImpression") } func bannerAdDidClick(_ banner: VungleBanner) { print("bannerAdDidClick") } func bannerAdWillLeaveApplication(_ banner: VungleBanner) { print("bannerAdWillLeaveApplication") } func bannerAdWillClose(_ banner: VungleBanner) { print("bannerAdWillClose") } func bannerAdDidClose(_ banner: VungleBanner) { print("bannerAdDidClose") } }
#pragma mark - VungleBanner Delegate Methods // Ad load Events - (void)bannerAdDidLoad:(VungleBanner *)banner { NSLog(@"bannerAdDidLoad"); } - (void)bannerAdDidFailToLoad:(VungleBanner *)banner withError:(NSError *)withError { NSLog(@"bannerAdDidFailToLoad"); } // Ad Lifecycle Events - (void)bannerAdWillPresent:(VungleBanner *)banner { NSLog(@"bannerAdWillPresent"); } - (void)bannerAdDidPresent:(VungleBanner *)banner { NSLog(@"bannerAdDidPresent"); } - (void)bannerAdDidFailToPresent:(VungleBanner *)banner withError:(NSError *)withError { NSLog(@"bannerAdDidFailToPresent"); } - (void)bannerAdDidTrackImpression:(VungleBanner *)banner { NSLog(@"bannerAdDidTrackImpression"); } - (void)bannerAdDidClick:(VungleBanner *)banner { NSLog(@"bannerAdDidClick"); } - (void)bannerAdWillLeaveApplication:(VungleBanner *)banner { NSLog(@"bannerAdWillLeaveApplication"); } - (void)bannerAdWillClose:(VungleBanner *)banner { NSLog(@"bannerAdWillClose"); } - (void)bannerAdDidClose:(VungleBanner *)banner { NSLog(@"bannerAdDidClose"); }
测试广告
您可以通过两种方式来测试广告: 1.将您的应用在Dashboard中设置为Test Mode。 2. 在Dashboard中,将您的设备广告ID添加到应用的Test Device列表中。详情请查看 Test Your Integration: Test Mode and Test Devices.
更多
- Refer to our Banner ad examples in Swift or Objective-C.
- Refer to our MREC ad examples in Swift or Objective-C.