Reputation: 59
.h
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#import "GADBannerView.h"
@interface MasterTableViewController : UITableViewController <GADBannerViewDelegate >{
GADBannerView *bannerView_;
}
@end
.m
#import "MasterTableViewController.h"
@interface MasterTableViewController ()
@end
@implementation MasterTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error
{
NSLog(@"Ad failed");
bannerView_.hidden = YES;
}
- (void)adViewDidRecieveAd:(GADBannerView *)view
{
NSLog(@"Ad recieved");
bannerView_.hidden = NO;
}
- (void) repeatAdRequest
{
[bannerView_ loadRequest:[GADRequest request]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[bannerView_ setDelegate:self];
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
bannerView_.adUnitID = @"ID_HERE";
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
}
The problem is, when I completely disable the network on my phone no ad shows up (obviously) but it also does not throw the didFailToReceiveAdWithError
. Then, I go to re-enable my network on my phone and nothing happens. The ad doesn't get refreshed if it fails? I've looked on many other posts that claimed it could be fixed with [bannerView_ setDelegate:self];
, that the app would throw when it received ads or not. But I can't seem to figure it out...does anyone have any ideas?
Upvotes: 0
Views: 253
Reputation: 47819
The problem is that you're trying to set the delegate on a nil object ... before you allocate it. The correct order of operations is
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
[bannerView_ setDelegate:self];
Upvotes: 1