AdMob广告未在UIView iPhone中显示

时间:2012-10-25 15:49:56

标签: iphone ios admob

我想在我的应用中添加AdMob横幅! 我让它在UIView控制器中工作,但不是在我的UIView ...横幅显示但是当你点击它时,没有任何反应!如果你点击UIViewcontrollers中的横幅它就可以了!

UIViewcontrollers中的代码(有效!)

 - (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.

}

UiView中的代码:(哪些不起作用)

 - (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
  }

行bannerView_.rootViewController = self;给了我一个警告,但没有它就行不通!警告说"不兼容的指针类型分配给' UIViezController *' blandad * const_strong'

blandad,名字是.IV和.h文件,用于UIView!

你认为错在哪里?

/ A菜鸟

4 个答案:

答案 0 :(得分:2)

请从- (void)drawRect:(CGRect)rect方法中删除所有代码,如果要在图形上下文中绘制内容,则必须覆盖此方法。此方法不会被调用多次,因此每次您将分配新视图并将其添加为子视图

如果您想将广告横幅添加到自定义UIView,请执行以下操作:

// 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;
}

您可以使用您拥有的任何控制器设置UIViewController* root,最好设置一个控制器,该视图是您的自定义视图的父级。我刚从app delegate为root视图控制器编写了一个路径,如果你想使用它,请确保你的app delegate有这个viewController属性。

答案 1 :(得分:0)

我不知道你是否解决了你的问题,但从我的角度来看,我得到了同样的问题。

将GAD横幅视图添加到从UIView继承的正确视图中,然后横幅显示良好,但Admob链接未激活。

在我的情况下,GAD横幅视图已添加到GADBannerView的adViewDidReceiveAd委托中。显然这不是好方法。

通过在初始化后直接添加GAD横幅视图,现在一切正常。

- (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];
}

在GADBannerView的adViewDidReceiveAd委托中,现在我只需要一个动画来修改横幅位置以获得动态视觉效果。

希望这可以帮助某人

答案 2 :(得分:0)

答案 3 :(得分:0)

我认为最好的方法是管理单个横幅广告,并将其添加到您需要广告的所有视图中。

试试我写的这门课程 - https://github.com/jackwu95/JWGADManager

相关问题