通过CocoaPod方式在ios平台集成ADMob

最初打算直接将admob的framework继承到项目中去,喜闻乐见的出现了非常多的链接错误,由于admob支持通过admob集成,所以换成该方式集成进项目。关于CocoaPods网上有非常多的介绍,这里就不再赘述了

通过Cocoapod集成Admob

1.安装Cocoapod

如果已经安装好了可以跳过这一部步

sudo gem install cocoapods

2.创建podfile

在xcode的项目根目录下创建一个文件,内容大致如下:

source ‘https://github.com/CocoaPods/Specs.git
platform :ios, ‘7.0’
pod ‘Google-Mobile-Ads-SDK’, ‘~> 7.5’

source:指定pod的下载源
platform:指定引入的工程使用平台版本
pod:指定引入工程和工程的版本

3.下载关联的库

关闭刚才的xcode项目的窗口,在项目的同级目录下,执行

pod update

pod update成功后,工程目录下多出了xxx.xcworkspace, Podfile.lock,Pods几个文件或文件夹,使用Xcode打开xxx.xcworkspace, 然后编译

4.Admob部分api的使用

(1).初始化

//传入的app-id为向google申请到的appid
[GADMobileAds configureWithApplicationID:@"app-id"];

(2).BannerAd的使用

BannerAd是一个小矩形的广告区域,通常长期占据着屏幕的某个位置

    _frame = frame;
    //创建视图
    adBannerView = [[GADBannerView alloc] initWithFrame:CGRectMake(0,frame.size.height IADVIEWHEIGHT,frame.size.width, IADVIEWHEIGHT)];
    //设置横幅广告的id
    adBannerView.adUnitID = ADMOBID;
    adBannerView.rootViewController = controller;
    //设置代理对象,广告的生命周期状态更新时,会回掉到相应的方法
    [adBannerView setDelegate:self];
    //将视图添加到场景
    [controller.view addSubview:adBannerView];
    [controller.view bringSubviewToFront:adBannerView];
    GADRequest *request = [GADRequest request];
    //开始请求广告
    [adBannerView loadRequest:request];```

(3).InterstitialAd的使用

插页广告通常会需要一点加载时间(视频广告加载时间更长),所以分成了加载广告,和播放广告两个步骤

    - (void)loadAdInterstitialView{
        adInterstitial =
        [[GADInterstitial alloc] initWithAdUnitID:@"interstitial-ad-id"];
        [adInterstitial setDelegate:self];
        GADRequest *request = [GADRequest request];
        // Request test ads on devices you specify. Your test device ID is printed to the console when
        // an ad request is made.
        request.testDevices = @[ kGADSimulatorID, @"test-device-id" ];
        [adInterstitial loadRequest:request];
    }
    - (void)showAdInterstitialView{
        if (adInterstitial.isReady) {
            [adInterstitial presentFromRootViewController:rootViewController];
        } else {
            NSLog(@"==================InterstitialAd is not ready");
        }
    }

(4).视频广告的使用

    - (void)loadAdRewardedVideo{
        [GADRewardBasedVideoAd sharedInstance].delegate = self;
        if(![[GADRewardBasedVideoAd sharedInstance] isReady]){
            NSLog(@"==================Appcontroller loadAdRewardedVideo");
            GADRequest *request = [GADRequest request];
            request.testDevices = @[ kGADSimulatorID, @"test-device-id" ];
            [[GADRewardBasedVideoAd sharedInstance] loadRequest:request
                                                   withAdUnitID:@"rewarded-ad-id"];
        }else{
    //        [self loadAdRewardedVideo:block];
        }
    }
    - (void)showAdRewardedVideo{
        if([[GADRewardBasedVideoAd sharedInstance] isReady]){
            [[GADRewardBasedVideoAd sharedInstance] presentFromRootViewController:rootViewController];
        }else{
            [self loadAdRewardedVideo];
            NSLog(@"=================RewardedVideo is not ready");
        }
    }

5.以下是我在集成的时候遇到的一些问题

(1). 在编译时,报错:library not found for -lPods,做了以下尝试

1.添加pod的search path
FrameWork Search Path:”{PODS_ROOT}/Google-Mobile-Ads-SDK/Frameworks/frameworks”
Header Search Path:”${PODS_ROOT}/Headers/Public”,”{PODS_ROOT}/Headers/Public/Google-Mobile-Ads-SDK”

2.添加Runpath Search Path:@executable_path/Frameworks (好像没用)

3.Product-Scheme-Edit Scheme-Build-点击+号添加Pod的工程
然后步骤4的报错没有了,变成另外的错误,是由于引入的Pod工程Architecture的问题,最后在Build Setting中,
将Build Active Architecture Only 设置为true,编译通过

(2).@import module_name 这种方式不支持c++,oc混编

1.将引入方式改为#import

(3).Objective-C中的block使用

在block中不能随意修改上下文中的变量,被修改的变量需要用__block进行修饰,否则会造成崩溃

(4).如何通过C++代码控制广告的表现

由于项目的原因,不能将Admob的相关代码直接写在.mm文件中,所以只好专门创建一个m实现一个单利类,实现Admob的逻辑,然后再创建一个mm创建一个c++类,对前面所说的单利进行引用,最后在cpp文件中引用mm的头文件,实现对广告表现的控制(有点绕,还专门写一个类来桥接,不过水平有限,暂时没想到好的办法)

猜你喜欢

转载自blog.csdn.net/a185368204/article/details/78947579