Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

ios - Shared iAd banner bannerViewDidLoadAd not being called

I am using the following code to setup a shared iAd banner.

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 is getting called as expected but bannerViewDidLoadAd is never called. I am trying to move some buttons up on the screen when the iAd banner loads.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your shared banner does not appear to be just one ADBannerView. It looks like you've set multiple @property's for your ADBannerView in your AppDelegate.h and your ViewController.h. Also, self.canDisplayBannerAds = true is creating an entirely new and different ADBannerView for you. self.canDisplayBannerAds = true can be used for a no hassle way of implementing iAds in your application. This will create a ADBannerView for you and show or hide the ADBannerView depending on whether it receives an ad or not from the iAd network. You will want to remove this from your viewDidLoad if you plan to implement a ADBannerView yourself.

Here is what your implementation of your shared ADBannerView should look like:

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];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...