看看我写的这段代码,基本上做同样的事情。真的请注意,这是我的一个项目的简单复制粘贴,但我认为这个概念得到了理解。问有什么不清楚的!基本情况是我有一个名为 iAd 的横幅bannerView
和一个名为noIADImageView
. 如果 iAd 无法加载,我会将其隐藏以支持noIADImageView
横幅设置(从例如 viewDidLoad 调用):
- (void) setupTheAdBanners {
// Adding the killer whales ad-banner if the iAds fail to provide an ad.
[self setupKillerWhalesBanner];
[bannerView setHidden:YES];
[noIADImageView setHidden:NO];
[bannerView setDelegate:self];
// and make sure it's positioned onscreen.
bannerView.frame = CGRectMake(0.0, 0.0, bannerView.frame.size.width, bannerView.frame.size.height);
noIADImageView.frame = bannerView.frame;
// bring your bannerView to the front
[self.view bringSubviewToFront:bannerView];
[self.view bringSubviewToFront:noIADImageView];
}
- (void) setupKillerWhalesBanner {
//Adding the banner as a subview
[self.view addSubview:bannerView];
// Initializing the UIImageView that will show only if the iAd do not display any ads
CGRect frameBan = CGRectMake(bannerView.frame.origin.x,bannerView.frame.origin.y-bannerView.frame.size.height,bannerView.frame.size.width,bannerView.frame.size.height);
noIADImageView = [[UIImageView alloc] initWithFrame:frameBan];
NSLog(@"%f,%f,%f,%f",bannerView.frame.origin.x,bannerView.frame.origin.y-bannerView.frame.size.height,bannerView.frame.size.width,bannerView.frame.size.height);
[noIADImageView setImage:[UIImage imageNamed:(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? @"KillerWhalesBanner-iPad.png" : @"KillerWhalesBanner-iPhone.png")]];
// ...as hidden
[noIADImageView setHidden:NO];
// Setting up a Tap regognizor for the UIImageView
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(clickEventOnImage:)];
[tapRecognizer setNumberOfTouchesRequired:1];
[tapRecognizer setDelegate:self];
// Enables user interaction to the UIImageView
noIADImageView.userInteractionEnabled = YES;
[noIADImageView addGestureRecognizer:tapRecognizer];
}
-(void)clickEventOnImage:(id) sender { // Open the Killer Whales app.
NSLog(@"Clicked banner");
[self playClickSound];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/us/app/killer-whales/id503511808?mt=8"]];
}
当横幅无法加载时,我将其隐藏以支持noIADImageView
as
-(void) bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
// Failed to load a banner
NSLog(@"Error with message: %@",error);
if(error) { //Some sort of error
[banner setHidden:YES];
[noIADImageView setHidden:NO];
}
}