共享的 iAd 横幅 bannerViewDidLoadAd 未被调用

Shared iAd banner bannerViewDidLoadAd not being called

我正在使用以下代码设置共享 iAd 横幅。

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    _adView = [[ADBannerView alloc]init];
}

ViewController.m

-(void) viewWillAppear:(BOOL)animated {
    AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication ]delegate];
    _adView = [appdelegate adView];
    _adView.delegate = self;
    self.canDisplayBannerAds = true;
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];
    [banner setAlpha:1];
    [UIView commitAnimations];
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];
    [banner setAlpha:0];
    [UIView commitAnimations];
}

bannerView:didFailToReceiveAdWithError 按预期被调用,但 bannerViewDidLoadAd 从未被调用。当 iAd 横幅加载时,我试图在屏幕上向上移动一些按钮。

您的共享横幅似乎不止一个 ADBannerView。您似乎在 AppDelegate.hViewController.h 中为 ADBannerView 设置了多个 @property。此外,self.canDisplayBannerAds = true 正在为您创建一个全新的 不同的 ADBannerViewself.canDisplayBannerAds = true 可用于 轻松 在您的应用程序中实施 iAds 的方式。这将为您创建一个 ADBannerView,并根据它是否从 iAd 网络接收到广告来显示或隐藏 ADBannerView。如果您打算自己实施 ADBannerView,则需要将其从 viewDidLoad 中删除。

共享 ADBannerView 的实现应该如下所示:

AppDelegate.h

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

@interface AppDelegate : UIResponder <UIApplicationDelegate> {

}

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ADBannerView *iAdView;

@end

AppDelegate.m

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    _iAdView = [[ADBannerView alloc]init];
    return YES;
}

ViewController.h

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

@interface ViewController : UIViewController <ADBannerViewDelegate>

@end

ViewController.m

#import "ViewController.h"

@interface ViewController  ()

@end

@implementation ViewController  {
    AppDelegate *appDelegate;
}

-(void)viewDidLoad {
    [super viewDidLoad];

    appDelegate = (AppDelegate *)[[UIApplication sharedApplication ]delegate];
    appDelegate.iAdView.delegate = self;
    appDelegate.iAdView.frame = CGRectMake(0, 0, appDelegate.iAdView.frame.size.width, appDelegate.iAdView.frame.size.height);
    [self.view addSubview:appDelegate.iAdView];

    // You created another adView property in your ViewController.h?
    //_adView = [appdelegate adView];
    //_adView.delegate = self;

    // This will actually create ANOTHER ADBannerView
    // Do not use when creating your own ADBannerView
    //self.canDisplayBannerAds = true;
}

-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
    NSLog(@"iAd LOADED");
    [UIView beginAnimations:nil context:NULL];
    appDelegate.iAdView.alpha = 1.0;
    [UIView commitAnimations];
}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
    NSLog(@"iAd FAILED");
    [UIView beginAnimations:nil context:NULL];
    appDelegate.iAdView.alpha = 0.0;
    [UIView commitAnimations];
}