gpt4 book ai didi

ios - 如何以编程方式在 iOS 的 iAds 横幅上显示广告

转载 作者:行者123 更新时间:2023-12-01 16:26:53 24 4
gpt4 key购买 nike

这是我第一次集成 iAds 的经验。我使用了以下链接

http://codewithchris.com/iad-tutorial/

我完美地实现了它。我的应用程序显示 AddBannerView 完美,但广告隐藏并显示不工作。我通过使用 Storyboard 添加了 adBannerView 并连接了它的代表和 IBOutlets。

-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
if (! bannerIsVisible) {
// If banner isn't part of view hierarchy, add it
if (advertiseView.superview == nil) {
[self.view addSubview:advertiseView];
}

[UIView beginAnimations:@"animateAdBannerOn" context:NULL];

// Assumes the banner view is just off the bottom of the screen.
banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);

[UIView commitAnimations];

bannerIsVisible = YES;
}
}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
NSLog(@"Failed to retrieve ad");

if (bannerIsVisible) {
[UIView beginAnimations:@"animateAdBannerOff" context:NULL];

// Assumes the banner view is placed at the bottom of the screen.
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);

[UIView commitAnimations];

bannerIsVisible = NO;
}
}

enter image description here

最佳答案

您遵循的教程已过时。你的原因ADBannerView在一段时间内出现在您的视野中间是因为您的 CGRectOffset在您的 ADBannerView的委托(delegate)方法。我猜你的 bannerIsVisible 有问题BOOL .

另外,不要使用 beginAnimations:context:方法。来自 UIView Class Reference :

Use of this method is discouraged in iOS 4.0 and later. You should use the block-based animation methods to specify your animations instead.



下面是一个如何实现 ADBannerView 的示例。以编程方式。此示例为 alpha 设置动画。 ADBannerView 的属性(property)显示或隐藏它。无需设置 ADBannerView的框架。它会知道它在哪个设备上并适本地调整自己的大小。只需设置它的位置就足够了。
#import "ViewController.h"
@import iAd; // Import iAd

@interface ViewController () <ADBannerViewDelegate> { // Include delegate
ADBannerView *adView; // Create globally
}

@end

@implementation ViewController

-(void)viewDidLoad {
[super viewDidLoad];
adView = [[ADBannerView alloc]init]; // Alloc/init
// Position
adView.center = CGPointMake(self.view.frame.size.width / 2,
self.view.frame.size.height - adView.frame.size.height / 2);
adView.delegate = self; // Set delegate
adView.alpha = 0.0; // Hide banner initially
[self.view addSubview:adView]; // Add to view
}

-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
// Delegate method called when the ADBannerView receives an ad
NSLog(@"bannerViewDidLoadAd");

// Animate alpha change to show ADBannerView
[UIView animateWithDuration:1.0 animations:^{
adView.alpha = 1.0;
}];
}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
// Delegate method called when the ADBannerView fails
// Can fail for multiple reasons so lets print why its failing in our NSLog
NSLog(@"didFailToReceiveAdWithError: %@", error);

// Animate alpha change to hide ADBannerView
[UIView animateWithDuration:1.0 animations:^{
adView.alpha = 0.0;
}];
}

关于ios - 如何以编程方式在 iOS 的 iAds 横幅上显示广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34790471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com