我正在尝试使用以下代码以编程方式创建横幅广告:
+ (void)createBanner:(UIViewController *)sender
{
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
{
bannerHeight = 50;
}
else
{
bannerHeight = 90;
}
GADRequest *request = [GADRequest request];
// request.testDevices = @[ @"testDeviceNumberIDNumber", @"Simulator"];
// request.testDevices = [NSArray arrayWithObjects:GAD_SIMULATOR_ID, nil];
bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
bannerView.adUnitID = @"MY ID"; // I have replaced my AdMob ID for posting this question with "MY ID"; usually the "ca-app-pub-xxxxx.." ID is in this line
bannerView.rootViewController = (id)self;
bannerView.delegate = (id<GADBannerViewDelegate>)self;
senderView = sender.view;
bannerRect = [bannerView bounds];
bannerView.frame = CGRectMake(0, 100, senderView.frame.size.width, bannerHeight);
[bannerView loadRequest:request];
containerView = [[UIView alloc] initWithFrame:senderView.frame];
bannerContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, senderView.frame.size.height, senderView.frame.size.width, bannerHeight)];
for (id object in sender.view.subviews) {
[object removeFromSuperview];
[containerView addSubview:object];
}
[senderView addSubview:containerView];
[senderView addSubview:bannerContainerView];
}
+ (void)adViewDidReceiveAd:(GADBannerView *)view
{
[UIView animateWithDuration:0.5 animations:^{
containerView.frame = CGRectMake(0, 0, senderView.frame.size.width, senderView.frame.size.height - bannerHeight);
bannerContainerView.frame = CGRectMake(0, senderView.frame.size.height - bannerHeight, senderView.frame.size.width, bannerHeight);
[bannerContainerView addSubview:bannerView];
}];
}
当我运行它时,此代码在我的一个旧应用程序中运行,但是当我在我的新应用程序中运行它时,它拒绝加载,无论我是否使用测试广告运行它。
现在有一个潜在的重要细节:在ViewController
的.h文件中,接口行读取@interface ViewController : UIViewController<UIAlertViewDelegate>
,但视图控制器嵌入在Navigation Controller
中,如下所示我加载了应用程序,顶部有一个导航栏。
这是不加载横幅的原因,如果是,我该如何解决?
所有帮助表示赞赏。
答案 0 :(得分:0)
我记得通过缓存集合中的bannerView
对象来处理这个问题。
代码中的某处:
@property (strong, nonatomic) NSMutableArray * googleAdsCollection;
// in viewDidLoad
- (void)viewDidLoad {
self.googleAdsCollection = [NSMutableArray array];
// ... your code here ...
}
- (void)createBanner {
// in your create banner perhaps at the end
[self.googleAdsCollection addObject:bannerView];
// ... your cod here ...
}
// inside the adView delegates - received, failed, what else
[self.googleAdsCollection removeObject:view]