How do I initialize Admob and set the subview

2019-08-22 16:41发布

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

2条回答
孤傲高冷的网名
2楼-- · 2019-08-22 17:23

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
查看更多
放我归山
3楼-- · 2019-08-22 17:27

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.

查看更多
登录 后发表回答