今天的主要工作放在了Admob上,插播广告是增加APP收入的另一种途径,那么Google的广告是不错的选择之一。
Admob现在做的越来越好,更加方便集成,文档齐全,而且基本也提供了中文文档。唯一的缺陷就是需要翻墙。
今天主要聊聊原生广告,其实思路是很简单的,和我们通常开发的思路一样,就是:
- 从网络上获取数据
- 将数据进行展示
GADAdLoader 负责从网络上加载数据,和其他广告不同,原生广告的数据可以提前加载并可以请求多个,不过需要注意的是,GADAdLoader需要指定一个rootViewController,我猜是用来记录广告是否加载的,所以我们不可以像数据类一样单独封装。
下面这段代码非常直观,即定义了一个GADAdLoader并请求数据,在代理中处理回调的数据。
@interface ViewController () <GADNativeAdLoaderDelegate, GADVideoControllerDelegate>
@property(nonatomic, strong) GADAdLoader *adLoader;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
GADMultipleAdsAdLoaderOptions *multipleAdsOptions =
[[GADMultipleAdsAdLoaderOptions alloc] init];
multipleAdsOptions.numberOfAds = 5;
self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:YOUR_AD_UNIT_ID
rootViewController:self
adTypes:@[kGADAdLoaderAdTypeNative]
options:@[multipleAdsOptions]];
self.adLoader.delegate = self;
[self.adLoader loadRequest:[GADRequest request]];
}
- (void)adLoader:(GADAdLoader *)adLoader
didReceiveNativeAd:(GADNativeAd *)nativeAd {
// A native ad has loaded, and can be displayed.
}
- (void)adLoaderDidFinishLoading:(GADAdLoader *) adLoader {
// The adLoader has finished loading ads, and a new request can be sent.
}
- (void)adLoader:(GADAdLoader *)adLoader
didFailToReceiveAdWithError:(NSError *)error;
@end
有了数据,接下来就是如何展示了。Admob定义了一个类,即GADNativeAdView,从下面的定义,我们可以看到定义的视图内容和我们获取的数据一一对应。我们需要做的就是将这些子视图按自己的风格进行布局并展示数据。
@interface GADNativeAdView : UIView
/// This property must point to the native ad object rendered by this ad view.
@property(nonatomic, strong, nullable) GADNativeAd *nativeAd;
/// Weak reference to your ad view's headline asset view.
@property(nonatomic, weak, nullable) IBOutlet UIView *headlineView;
/// Weak reference to your ad view's call to action asset view.
@property(nonatomic, weak, nullable) IBOutlet UIView *callToActionView;
/// Weak reference to your ad view's icon asset view.
@property(nonatomic, weak, nullable) IBOutlet UIView *iconView;
/// Weak reference to your ad view's body asset view.
@property(nonatomic, weak, nullable) IBOutlet UIView *bodyView;
/// Weak reference to your ad view's store asset view.
@property(nonatomic, weak, nullable) IBOutlet UIView *storeView;
/// Weak reference to your ad view's price asset view.
@property(nonatomic, weak, nullable) IBOutlet UIView *priceView;
/// Weak reference to your ad view's image asset view.
@property(nonatomic, weak, nullable) IBOutlet UIView *imageView;
/// Weak reference to your ad view's star rating asset view.
@property(nonatomic, weak, nullable) IBOutlet UIView *starRatingView;
/// Weak reference to your ad view's advertiser asset view.
@property(nonatomic, weak, nullable) IBOutlet UIView *advertiserView;
/// Weak reference to your ad view's media asset view.
@property(nonatomic, weak, nullable) IBOutlet GADMediaView *mediaView;
/// Weak reference to your ad view's AdChoices view. Must set adChoicesView before setting
/// nativeAd, otherwise AdChoices will be rendered according to the preferredAdChoicesPosition
/// defined in GADNativeAdViewAdOptions.
@property(nonatomic, weak, nullable) IBOutlet GADAdChoicesView *adChoicesView;
@end
通常的案例就是在TableView中展示自己内容的时候,每隔几个Cell的内容展示,插入一条类似Cell风格的广告。
此时另一个问题是,如何刷新广告,我们知道横幅广告是每隔一段时间自动刷新的,但原生广告是不会刷新的,需要我们在某一个时刻手动重新下载数据。目前我大概的思路是这样的:
- 一般内容的展示,我们也是按页向服务器请求,例如一次请求10条数据,最简单的就是,每次请求数据的时候,也请求一个广告
- 如果打算预加载多条广告,例如5条,那么也同理,在请求0/5/10页内容的时候,请求广告。
总结:
今日的推荐就是一个广告类的第三方库:Google Admob
推荐理由:功能强大,提供横幅,插页,原生,激励等各种广告的集成。文档齐全,且有中文文档,更新及时,集成简单,非常容易上手。唯一的缺陷就是需要翻墙。
网友评论