Apple 的 BannerView 示例应用程序似乎涵盖了这一点。它不是故事板应用程序,但应该适用基本规则。有一个 VC 拥有广告视图和您的应用程序的视图。
我在这里写了一篇关于它的博客文章:http://www.notthepainter.com/iad-admob-integration-with-a-dynamic-uiview/
请注意,博客文章是关于 iAd 和 ad mob 集成的,但应该适用相同的概念。您根本不需要使用广告集成来完成此操作。你真的可以忽略这个答案的其余部分,看看 Apple 是如何使用 BannerView 示例应用程序完成的。
这是博客文章:
已经发布了 2 个带有 iAd 的应用程序。我使用 Apple 的 BannerView 示例代码来实现这一点。基本上,在您的委托中,您不会将 root 设置为您预期的 root UIViewController,而是将 root 设置为包含您真正的 root 的 BannerView。当 iAd 可用时,您的主视图会缩小并且 iAd 显示在底部。当广告不可用时,您的视图会扩大到其“正常”尺寸。
这在测试中非常有效,因此我将这两个应用程序都发布到了应用商店。然而,当我第一次从商店下载这些版本时,我很惊讶地发现从来没有广告。事实证明,至少现在,iAd 的填充率非常糟糕。所以我想在 iAd 不可用时再展示一个广告。
我在 GitHub 上找到了 larsacus 的开源项目 LARSAdController。除了一件事,他使广告集成变得非常容易。当你沿着他的快速发展路线走时,你会看到广告覆盖你的视野,它不会缩小以适应广告。这是一个完全合理的设计决定,而不是我想要的。
所以我决定修改 Apple 的 BannerView 以使用 LARSAdController。这很容易。
您要做的第一件事是从 BannerView 的 .h 文件中删除 iAd 并在 LARS TOLAdViewController 类中删除广告。
#import "TOLAdViewController.h"
#define KVO_AD_VISIBLE @"KVO_AD_VISIBLE"
@interface BannerViewController : TOLAdViewController
(暂时忽略 KVO_AD_VISIBLE 定义,稍后我会介绍。)在 .m 文件中,还要删除 iAd 并进行以下更改:
@implementation BannerViewController {
UIView *_bannerView;
UIViewController *_contentController;
BOOL isLoaded;
}
我们将 _bannerView 从 ADBannerView 更改为普通的旧 UIVIew。 ADBannerView 还有一个bannerLoaded 属性,我们必须用我们的isLoaded 布尔值替换它。 initWithContentViewController 也很容易修改。
// IAPHelper *sharedInstance = [//IAPHelper sharedInstance];
//if ([sharedInstance showBannerAds]) {
if (YES) {
_bannerView = [[UIView alloc] initWithFrame:CGRectZero];
} else {
_bannerView = nil; // not showing ads since the user has upgraded
}
注意注释掉的部分。如果您使用应用内购买将支持广告的版本转换为无广告版本,您可以在此处进行。
在该方法的最后,使用 Key-Value-Observing (KVO) 观察 LARS 并查看何时投放或删除广告。 (我可能会在以后的博客文章中介绍 KVO。)
[[LARSAdController sharedManager] addObserver:self
forKeyPath:kLARSAdObserverKeyPathIsAdVisible
options:0
context:KVO_AD_VISIBLE]
以及观察代码:
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context;
{
if(context == KVO_AD_VISIBLE) {
NSNumber *isVisible = [object valueForKey:kLARSAdObserverKeyPathIsAdVisible];
if ([isVisible boolValue]) {
_bannerView.frame = [[LARSAdController sharedManager] containerView].frame;
isLoaded = YES;
} else {
isLoaded = NO;
}
}
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
}
我们保存新广告的框架并更新 isLoaded 变量。 (原本以为我需要调用 setNeedsLayout 和 layoutIfNeeded,但实际上我没有。) viewDidLayoutSubviews 的模组也非常简单。唯一奇怪的部分是删除了对 ADBannerContentSizeIdentifierPortrait 和 ADBannerContentSizeIdentifierLandscape 的引用,并将其全部替换为一行:
bannerFrame.size = [_bannerView sizeThatFits:contentFrame.size];
几行之后你使用新的 isLoaded 变量
if (isLoaded) {
别忘了在 dealloc 中清理你的观察者:
-(void) dealloc;
{
[[LARSAdController sharedManager] removeObserver:self forKeyPath:kLARSAdObserverKeyPathIsAdVisible];
}
剩下的就是在你的应用代理中告诉 LARS 你的广告:
[[LARSAdController sharedManager] registerAdClass:[TOLAdAdapterGoogleAds class] withPublisherId:@"a14e55c99c24b43"];
[[LARSAdController sharedManager] registerAdClass:[TOLAdAdapteriAds class]];
LARSBannerViewController *root = [[LARSBannerViewController alloc] initWithNibName:@"LARSBannerViewController" bundle:nil];
_bannerViewController = [[BannerViewController alloc] initWithContentViewController:root];
[self.window setRootViewController:_bannerViewController];
就是这样。现在您的应用应该显示 iAD 或 AdMob 广告,并且您的视图将缩小以适应它们。
当然有一个错误,我不知道这是在 AdMob 服务器中还是在 LARS 中,但是当您在 iPhone 上处于横向模式时,广告的尺寸和报告的尺寸不同,会在底部留下一个黑条屏幕。我已经 ping 过 larsacus,当我知道更多信息时会更新这篇文章。