简体   繁体   English

如何在iOS的每个视图上加载AdMob广告

[英]How to load AdMob ad on every view in iOS

I am currently loading this code below on every view load. 我目前正在每次视图加载时在下面加载此代码。 Is there a more performance direction I can take, like a singleton, or is this the best way to load ads in iOS? 我是否可以像单身汉一样采取其他性能方针,还是这是在iOS中加载广告的最佳方法?

- (void)viewDidLoad {
    buttonsView.orientation = CSLinearLayoutViewOrientationVertical;
    buttonsView.verticalAlignment = CSLinearLayoutItemVerticalAlignmentBottom;

    self.ad.adUnitID = @"ca-app-pub-3940256099942544/6300978111";
    self.ad.rootViewController = self;
    self.ad.delegate = self;
    self.ad.adSize = GADAdSizeFromNSValue(NSValueFromGADAdSize(kGADAdSizeSmartBannerPortrait));
    [self.ad loadRequest:[GADRequest request]];

    [super viewDidLoad];
}

There are a few ways you can do this. 有几种方法可以做到这一点。 One method is to create a UIViewController super class. 一种方法是创建UIViewController超类。 In the viewDidLoad method of the superclass implement the ad code. 在超类的viewDidLoad方法中,实现广告代码。 Any view which needs this ad code should just inherit from the super class and it get's this behavior. 任何需要此广告代码的视图都应继承自超类,并且它就是这种行为。 If each UIViewController has specific values to identify it as a unique view you can set this values in the before calling [super viewDidLoad] on super class. 如果每个UIViewController都有特定的值来将其标识为唯一视图,则可以在对超级类调用[super viewDidLoad]之前在中设置此值。 If this doesn't make any sense let me know and I can put some code together for you. 如果这没有任何意义,请告诉我,我可以为您添加一些代码。

Another possible approach is to use method swizzling to modify the default behavior of the UIViewController and add your code this way. 另一种可能的方法是使用旋转方法来修改UIViewController的默认行为,并以此方式添加代码。 Just google "method swizzling objective-C". 只是谷歌“方法令人毛骨悚然的目标-C”。 Here's one of the better articles on this that I like: https://blog.newrelic.com/2014/04/16/right-way-to-swizzle/ 这是我喜欢的更好的文章之一: https : //blog.newrelic.com/2014/04/16/right-way-to-swizzle/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM