How do I initialize Admob and set the subview

2019-08-22 16:22发布

问题:

I'm fairly new to xcode, but i'm having some trouble adding admob to my app.

I followed the instructions for Admob but the ads are not showing up.

Im thinking i need to add the new view i created AdViewController to appsdelegate. Is there a code i should add for that?

BTW: Im using a tabbar controller as the rootviewcontroller

回答1:

In the view controller in which you want to display the add , do the following :

.h

@interface MyViewController: UIViewController
{
  GADBannerView *adBanner;
}

@end

.m

@implementation MyViewController

// In the viewDidLoad method of the controller create a adbanner
-(void) viewDidLoad
{
  [super viewDidLoad];

  adBanner_ = [[GADBannerView alloc] initWithFrame:CGRectMake(0, 0, GAD_SIZE_320x50.width, GAD_SIZE_320x50.height)];
  adBanner_.delegate = self;
  adBanner_.rootViewController = self;
  adBanner_.adUnitID = the_ad_ID_given_by_Google;
  GADRequest *request = [GADRequest request];
  [adBanner loadRequest:request];

  [self.view addSubview:adBanner];
}

@end


回答2:

You need to set the rootViewController to be the UITabBarController, but you need to add the GADBannerView object into the view for the view Controller that's currently being shown.

There's an example using a custom UITabBar solution here.