123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- //
- // FqAdmobNativeAd.m
- // QQQ
- //
- // Created by Mgs on 2020/12/7.
- //
- #import "FqAdmobNativeAd.h"
- #import "XSNetwork.h"
- @interface FqAdmobNativeAd()<GADUnifiedNativeAdLoaderDelegate,GADVideoControllerDelegate,GADUnifiedNativeAdDelegate>
- @property(nonatomic,strong)GADUnifiedNativeAd *nativeAd;
- @property(nonatomic, strong) GADAdLoader *adLoader;
- @property(nonatomic, strong) GADUnifiedNativeAdView *nativeAdView;
- @property(nonatomic,strong)UIView *containerView;
- @end
- @implementation FqAdmobNativeAd
- - (instancetype)init
- {
- self = [super init];
- if (self) {
- self.agentName = @"admob"; //平台名称
- //@"ca-app-pub-3940256099942544/3986624511";
- NSDictionary *infos = [[NSBundle mainBundle] infoDictionary];
- self.adUnitId = [infos objectForKey:@"mob-native-unitId"];
- self.type = @"Native"; //广告类型
- self.unitAdId = @""; //广告组ID
- }
- return self;
- }
- -(GADAdLoader *)adLoader{
- if (!_adLoader) {
-
-
- //NSString *nativeAdId = @"ca-app-pub-3940256099942544/3986624511";
- UIViewController *vc = [GMTools getViewControl];
- GADVideoOptions *videoOptions = [[GADVideoOptions alloc] init];
- _adLoader = [[GADAdLoader alloc] initWithAdUnitID:self.adUnitId
- rootViewController:vc
- adTypes:@[ kGADAdLoaderAdTypeUnifiedNative ]
- options:@[ videoOptions ]];
- _adLoader.delegate = self;
- self.nativeAd = nil;
- }
- return _adLoader;
- }
- -(GADUnifiedNativeAdView *)nativeAdView{
- if (!_nativeAdView) {
- NSArray *nibObjects =
- [[NSBundle mainBundle] loadNibNamed:@"FqAdmobNativeAd" owner:nil options:nil];
- _nativeAdView = (GADUnifiedNativeAdView *)[nibObjects firstObject];
- _nativeAdView.backgroundColor = [UIColor whiteColor];
- }
- return _nativeAdView;
- }
- -(void)loadAd{
- self.isReady = false;
- dispatch_async(dispatch_get_main_queue(), ^{
- [self.adLoader loadRequest:[GADRequest request]];
- });
-
- //数据上报
- [XSNetwork adRecord:@"quest" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
-
- }
- -(void)openWithSuperView:(UIView *)container{
-
-
- self.containerView = container;
- if (self.nativeAd) {
-
- self.isReady = false;
-
- UIImageView *iconView = (UIImageView *)self.nativeAdView.iconView;
- iconView.image = self.nativeAd.icon.image;
-
- UILabel *titleLabel = (UILabel *)self.nativeAdView.headlineView;
- titleLabel.textColor = [UIColor blackColor];
- titleLabel.text = self.nativeAd.headline;
-
- UILabel *subTitleLabel = (UILabel *)self.nativeAdView.bodyView;
- subTitleLabel.textColor = [UIColor grayColor];
- subTitleLabel.text = self.nativeAd.body;
-
- UIButton *installBtn = (UIButton *)self.nativeAdView.callToActionView;
- // [installBtn setTitle:self.nativeAd.callToAction forState:0];
- installBtn.userInteractionEnabled = false;
-
- if (self.nativeAd.images.count>0) {
- UIImageView *contentImage = (UIImageView *)self.nativeAdView.imageView;
- contentImage.image = [self.nativeAd.images firstObject].image;
- }
-
- UIButton *closeBtn = (UIButton *)[self.nativeAdView viewWithTag:9901];
- [closeBtn addTarget:self action:@selector(closeAd) forControlEvents:UIControlEventTouchUpInside];
-
- self.nativeAdView.nativeAd = self.nativeAd;
- self.nativeAdView.frame = CGRectMake(0, 0, container.bounds.size.width, container.bounds.size.height);
- [container addSubview:self.nativeAdView];
-
- //数据上报
- [XSNetwork adRecord:@"show" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
- }
- }
- -(void)closeAd{
- self.isReady = false;
- dispatch_async(dispatch_get_main_queue(), ^{
- for (UIView *vi in self.containerView.subviews) {
- [vi removeFromSuperview];
- }
- self.containerView.hidden = true;
- });
-
- [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
-
- }
- #pragma mark GADAdLoaderDelegate implementation
- - (void)adLoaderDidFinishLoading:(nonnull GADAdLoader *)adLoader{
-
- }
- - (void)adLoader:(GADAdLoader *)adLoader didFailToReceiveAdWithError:(GADRequestError *)error {
- self.isReady = false;
- self.errorMsg = [NSString stringWithFormat:@"code=%ld,domain=%@,describe=%@",(long)error.code,error.domain,error.userInfo];
- }
- #pragma mark GADUnifiedNativeAdLoaderDelegate implementation
- - (void)adLoader:(GADAdLoader *)adLoader didReceiveUnifiedNativeAd:(GADUnifiedNativeAd *)nativeAd {
- self.isReady = true;
- self.nativeAd = nativeAd;
- }
- #pragma mark GADVideoControllerDelegate implementation
- - (void)videoControllerDidEndVideoPlayback:(GADVideoController *)videoController {
-
- }
- #pragma mark GADUnifiedNativeAdDelegate
- - (void)nativeAdDidRecordClick:(GADUnifiedNativeAd *)nativeAd {
- NSLog(@"%s", __PRETTY_FUNCTION__);
- [XSNetwork adRecord:@"click" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
-
- }
- - (void)nativeAdDidRecordImpression:(GADUnifiedNativeAd *)nativeAd {
- NSLog(@"%s", __PRETTY_FUNCTION__);
- }
- - (void)nativeAdWillPresentScreen:(GADUnifiedNativeAd *)nativeAd {
- NSLog(@"%s", __PRETTY_FUNCTION__);
- }
- - (void)nativeAdWillDismissScreen:(GADUnifiedNativeAd *)nativeAd {
- NSLog(@"%s", __PRETTY_FUNCTION__);
- }
- - (void)nativeAdDidDismissScreen:(GADUnifiedNativeAd *)nativeAd {
- NSLog(@"%s", __PRETTY_FUNCTION__);
- [XSNetwork adRecord:@"close" spaceId:self.adId agentName:self.agentName medium:self.medium adUnitId:self.adUnitId type:self.type unitAdId:self.unitAdId errorMsg:self.errorMsg];
- }
- - (void)nativeAdWillLeaveApplication:(GADUnifiedNativeAd *)nativeAd {
- NSLog(@"%s", __PRETTY_FUNCTION__);
- }
- @end
|