在 iOS 中实现横幅广告
重要提示:app-ads.txt 是 IAB 为打击欺诈和在广告生态系统中创造透明度而发起的一项倡议。确保您按照说明实施 app-ads.txt。否则,横幅广告需求可能会大幅减少。
横幅广告需要特定类型的专用横幅广告单元。
您可以将横幅视图对象放置到您的视图层次结构中,就像您放置任何其他视图一样。这允许您自定义每个横幅实例的位置,或显示多个横幅。
Note: Banner content will not render outside of the Safe Area, as described in Apple's guidelines.
脚本实现
在 ViewController
实现(.m
)中添加您的横幅代码。以下脚本示例是显示两个横幅广告的示例实现。有关引用类的更多信息,请参阅 UADSBannerView
API 部分。
实现 MREC 横幅格式
Note: You must initialize Unity Ads before displaying a banner ad.
要在您的应用中显示中等矩形 (MREC) 尺寸的广告格式,请确保使用正确的尺寸,例如 300x250、300x300 或 450x450。对于 300x250 MREC 横幅广告,请在以下示例代码中替换
CGSizeMake(320, 50)
使用此调整后的尺寸
CGSizeMake(300, 250)
示例代码
@interface ViewController () <UADSBannerViewDelegate>
// This is the Ad Unit or Placement that will display banner ads:
@property (strong) NSString* placementId;
// This banner view object will be placed at the top of the screen:
@property (strong, nonatomic) UADSBannerView *topBannerView;
// This banner view object will be placed at the bottom of the screen:
@property (strong, nonatomic) UADSBannerView *bottomBannerView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.placementId = @"banner";
[UnityAds initialize: @"1234567" testMode: YES initializationDelegate: self];
}
// Example method for creating and loading the top banner view object:
- (void)loadTopBanner{
// Instantiate a banner view object with the Ad Unit ID and size:
self.topBannerView = [[UADSBannerView alloc] initWithPlacementId: _placementId size: CGSizeMake(320, 50)];
// Set the banner delegate for event callbacks:
self.topBannerView.delegate = self;
// Add the banner view object to the view hierarchy:
[self addBannerViewToTopView:self.topBannerView];
// Load ad content to the banner view object:
[_topBannerView load];
}
// Example method for creating and loading the bottom banner view object:
- (void)loadBottomBanner{
self.bottomBannerView = [[UADSBannerView alloc] initWithPlacementId: _placementId size: CGSizeMake(320, 50)];
self.bottomBannerView.delegate = self;
[self addBannerViewToBottomView:self.bottomBannerView];
[_bottomBannerView load];
}
// Example method for discarding the top banner view object (for example, if there's no fill):
- (void)unLoadTopBanner{
// Remove the banner view object from the view hierarchy:
[self.topBannerView removeFromSuperview];
// Set it to nil:
_topBannerView = nil;
}
// Example method for discarding the bottom banner view object:
- (void)unLoadBottomBanner{
[self.bottomBannerView removeFromSuperview];
_bottomBannerView = nil;
}
// Example method for placing the top banner view object:
- (void)addBannerViewToTopView:(UIView *)bannerView {
bannerView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:bannerView];
[self.view addConstraints:@[
[NSLayoutConstraint constraintWithItem:bannerView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.topLayoutGuide
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0],
[NSLayoutConstraint constraintWithItem:bannerView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]
]];
}
// Example method for placing the bottom banner view object:
- (void)addBannerViewToBottomView: (UIView *)bannerView {
bannerView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:bannerView];
[self.view addConstraints:@[
[NSLayoutConstraint constraintWithItem:bannerView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.bottomLayoutGuide
attribute:NSLayoutAttributeTop
multiplier:1
constant:0],
[NSLayoutConstraint constraintWithItem:bannerView
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1
constant:0]
]];
}
// Implement the delegate methods:
#pragma mark : UADSBannerViewDelegate
- (void)bannerViewDidLoad:(UADSBannerView *)bannerView {
// Called when the banner view object finishes loading an ad.
NSLog(@"Banner loaded for Ad Unit or Placement: %@", bannerView.placementId);
}
- (void)bannerViewDidClick:(UADSBannerView *)bannerView {
// Called when the banner is clicked.
NSLog(@"Banner was clicked for Ad Unit or Placement: %@", bannerView.placementId);
}
- (void)bannerViewDidLeaveApplication:(UADSBannerView *)bannerView {
// Called when the banner links out of the application.
}
- (void)bannerViewDidError:(UADSBannerView *)bannerView error:(UADSBannerError *)error{
// Called when an error occurs showing the banner view object.
NSLog(@"Banner encountered an error for Ad Unit or Placement: %@ with error message %@", bannerView.placementId, [error localizedDescription]);
// Note that the UADSBannerError can indicate no fill (refer to the API documentation).
}
@end