大家好,我导入了 iAd 框架并为 iAd 实现了代码。我的示例代码如下所示

.h 文件

#import <UIKit/UIKit.h>
#import <iAd/iAd.h>

@interface IadTestViewController : UIViewController<ADBannerViewDelegate> {
BOOL isBannerVisible;
IBOutlet ADBannerView *banner;

}
@property(nonatomic,assign)BOOL isBannerVisible;
 @property(nonatomic,retain)IBOutlet ADBannerView *banner;

@end

.m 文件我实现了委托(delegate)方法
 #import "IadTestViewController.h"

@implementation IadTestViewController
@synthesize banner,isBannerVisible;


- (void)viewDidLoad {
[super viewDidLoad];
isBannerVisible=NO;
bannerView=[[ADBannerView alloc]initWithFrame:CGRectZero];
bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
bannerView.delegate=self;
[self.view addSubview:bannerView];

}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
 {
    if (!self.isBannerVisible)
{
    [UIView beginAnimations:@"animateAdBannerOn" context:NULL];
    // Assumes the banner view is just off the bottom of the screen.
    bannerView.frame = CGRectOffset(bannerView.frame, 0, -bannerView.frame.size.height);
    [UIView commitAnimations];
    self.isBannerVisible = YES;
}

}
  - (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
 {
NSLog(@"the failed error is %@",error);
if (self.isBannerVisible)
{
    [UIView beginAnimations:@"animateAdBannerOff" context:NULL];
    // Assumes the banner view is placed at the bottom of the screen.
    bannerView.frame = CGRectOffset(bannerView.frame, 0, bannerView.frame.size.height);
    [UIView commitAnimations];
    self.isBannerVisible = NO;
}

}

而且我的 xib 连接格式良好。仍然没有在我的模拟器中收到 IAd 横幅,我的 Log 语句给了我这样的错误
Error Domain=ADErrorDomain Code=5 "The operation couldn’t be completed. Banner view is visible but does not have content" UserInfo=0x574fdd0 {ADInternalErrorCode=5, NSLocalizedFailureReason=Banner view is visible but does not have content}
我知道当广告为零时 iAd 横幅不可见。但是我正在尝试显示测试广告,即使我的程序也无法做到这一点。我不知道我犯了什么错误或忘记实现哪一步。我在中看到了许多类似的问题我们的 stackoverflow.com 但没有一个答案能解决我的问题。任何人都可以帮我解决这个问题,请提供一些示例代码以在模拟器中显示 TestAdd。提前谢谢。

我得到了解决方案

我在下面的链接中提供了答案

Why does test iAd for barebones project not display?

最佳答案

步骤1:


@property (weak, nonatomic) IBOutlet UIView *contentView;

第2步:

在 ViewDidLoad 方法中分配
- (void)viewDidLoad
{
_bannerView = [[ADBannerView alloc] init];
_bannerView.delegate = self;

[super viewDidLoad];
[self.view addSubview:_bannerView];
}

第 3 步:

提供我在下面提到的委托(delegate)方法。
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
} else {
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
}
[self layoutAnimated:duration > 0.0];
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
[self layoutAnimated:YES];
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
[self layoutAnimated:YES];
}

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{

return YES;
}

- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{

}
- (void)layoutAnimated:(BOOL)animated
{
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
} else {
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
}

CGRect contentFrame = self.view.bounds;
CGRect bannerFrame = _bannerView.frame;
if (_bannerView.bannerLoaded) {
contentFrame.size.height -= _bannerView.frame.size.height;
bannerFrame.origin.y = contentFrame.size.height;
} else {
bannerFrame.origin.y = contentFrame.size.height;
}

[UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{
self.contentView.frame = contentFrame;
[self.contentView layoutIfNeeded];
_bannerView.frame = bannerFrame;
}];
}

欲了解更多详情,请参阅此 link

关于iphone - 如何在模拟器中显示测试 IAd 横幅,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5947552/

10-13 07:29