Mangy92
Mangy92

Reputation: 631

AdMob ad not showing up in UIView iPhone

I want to have AdMob banners in my app! I got it to work in the UIViewcontrollers, but not in my UIView... The banner shows up but when you click on it, nothing happens! If you click on the banners in the UIViewcontrollers it works!

Code in UIViewcontrollers (which works!)

 - (void)viewDidLoad
 {

[super viewDidLoad];
bannerView_ = [[GADBannerView alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height - GAD_SIZE_320x50.height-49, GAD_SIZE_320x50.width, GAD_SIZE_320x50.height)];

bannerView_.adUnitID = MY_BANNER_UNIT_ID;
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];


GADRequest *r = [[GADRequest alloc] init];
r.testing = YES;
[bannerView_ loadRequest:r];
// Do any additional setup after loading the view, typically from a nib.

}

Code in UiView: (Which doesn't work)

 - (void)drawRect:(CGRect)rect
  {




  // Create a view of the standard size at the bottom of the screen.
  bannerView_ = [[GADBannerView alloc] initWithFrame:CGRectMake(0.0, self.frame.size.height - GAD_SIZE_320x50.height, GAD_SIZE_320x50.width, GAD_SIZE_320x50.height)];
  // Specify the ad's "unit identifier." This is your AdMob Publisher ID.
  bannerView_.adUnitID = MY_BANNER_UNIT_ID;

  // Let the runtime know which UIViewController to restore after taking
  // the user wherever the ad goes and add it to the view hierarchy.
  bannerView_.rootViewController = self;
 [self addSubview:bannerView_];

  // Initiate a generic request to load it with an ad.
  [bannerView_ loadRequest:[GADRequest request]];
  // Drawing code
  }

The line bannerView_.rootViewController = self; gives me a warning, but it doesn't work without it! The warning says "Incompatible pointer types assigning to 'UIViezController *'from 'blandad *const_strong'

blandad in the name om the .m and .h file for the UIView!

What do you think is wrong?

/A noob

Upvotes: 1

Views: 2044

Answers (4)

Jack
Jack

Reputation: 16855

The best way in my opinion is to manage a single banner ad and add it to all the views that you need an Ad in.

Try this class I wrote - https://github.com/jackwu95/JWGADManager

Upvotes: 0

Lisarien
Lisarien

Reputation: 1136

I don't know if you resolved your issue but from my part I got the same one.

Adding the GAD banner view to my proper view inherited from UIView then the banner was well displayed but the Admob link was not active.

In my case the GAD banner view was added into the GADBannerView's adViewDidReceiveAd delegate. Apparently it's not the good way.

By adding the GAD banner view directly after initializing it then everything works fine now.

- (void)initGADBannerViewWithId:(NSString *)bannerId andRootController:(UIViewController *)rootController
{
    _gadBannerView = [[GADBannerView alloc] initWithFrame:self.frame];

    _gadBannerView.adUnitID = bannerId;

    // Let the runtime know which UIViewController to restore after taking
    // the user wherever the ad goes and add it to the view hierarchy.
    _gadBannerView.rootViewController = rootController;
    _gadBannerView.delegate = self;
    [self addSubview:_gadBannerView];
}

And in the GADBannerView's adViewDidReceiveAd delegate, now I simply have an animation modifying the banner position to have a dynamic visual effect.

Hope this can help someone

Upvotes: 0

Ezeki
Ezeki

Reputation: 1519

Please remove all that code from - (void)drawRect:(CGRect)rect method, you have to override this method in the case you want to draw something in the graphical context. This method will be called not once but many times, so every time you will allocate a new view and add it as a subview

In the case if you want to add the ad banner to custom UIView, just do something like this:

// in your CustomView implementation
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        bannerView_ = [[GADBannerView alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height - GAD_SIZE_320x50.height-49, GAD_SIZE_320x50.width, GAD_SIZE_320x50.height)];

        bannerView_.adUnitID = MY_BANNER_UNIT_ID;

        UIViewController* root = [(YOUR_APP_DELEGATE_CLASS*)[[UIApplication sharedApplication] delegate] viewController];
        bannerView_.rootViewController = root;
        [self addSubview:bannerView_];


        GADRequest *r = [[GADRequest alloc] init];
        r.testing = YES;
        [bannerView_ loadRequest:r];

    }
    return self;
}

You can set UIViewController* root with any controller you have, it is better to set there a controller which view is a parent of your custom view. I just wrote a path for root view controller from app delegate, if you want to use it please make sure that your app delegate has this viewController property.

Upvotes: 2

Related Questions