未调用共享的iAd横幅bannerViewDidLoadAd

时间:2015-06-06 01:03:40

标签: ios objective-c iad

我使用以下代码设置共享的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横幅加载时,我试图在屏幕上移动一些按钮。

1 个答案:

答案 0 :(得分:1)

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

以下是您的共享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];
}