Xcode使用Cocoapods集成Google 广告中介三方库

2020-05-19  本文已影响0人  Android大师哥

1.配置 ruby 环境

  1. 查看ruby源
gem sources –l
  1. 删除ruby源
gem sources --remove [https://rubygems.org/](https://rubygems.org/) 
  1. 添加ruby源
gem sources -a [https://gems.ruby-china.com/](https://gems.ruby-china.com/) 

注意:最新的 ruby 源已改为https://gems.ruby-china.com

2. 安装 CocoaPods

  1. 安装 CocoaPods 之前,如果 gem 版本太老,需要升级gem:
  2. 更新
sudo gem update --system
  1. 查看gem版本
gem -v
  1. 执行命令安装CocoaPods :
~% sudo gem install cocoapods

注意:

sudo gem update --system
    sudo gem install cocoapods -n /usr/local/bin
  1. 安装成功之后执行:pod setup

3.CocoaPods的使用

  1. 进入到项目的根目录,包含testPodfile,testPodfile.xcodeproj的文件夹


    image-20200519151344993.png
    image-20200519151936555.png
  2. 使用vim 创建Podfile文件,按i键进入编辑模式(随便一个键也行),输入下面的代码
source 'https://gitee.com/majy903/Specs.git'
platform :'ios', '10.0'
target 'testPodfile' do
pod 'GoogleMobileAdsMediationAppLovin'
pod 'GoogleMobileAdsMediationUnity'
pod 'GoogleMobileAdsMediationFacebook'
pod 'GoogleMobileAdsMediationTestSuite'
end

注意:
* 使用source指定源,避免下载三方库时间过长
* platform 指定版本
* target这个需要填写项目名
* pod指定你想要加载的三方库
编辑之后按esc键退出编辑模式,然后输入:wq,保存当前文件并退出

image-20200519155620812.png
image-20200519155647662.png

3.执行pod install --repo-update,如果报错提示repo需要权限,就只执行pod install也行,此处回去下载安装三方库,时间需根据实际网络情况而定。

图片如下所示:

image-20200519162532064.png

当出现上面的代码时就表示已经集成成功。此时需要重新打开xcode。首先找到项目的根目录,然后双击testPodfile.xcworkspace,白色图标那个。

4.代码集成

  1. 在google admob加载之前调用下面的代码
    [[GADMobileAds sharedInstance] startWithCompletionHandler:^(GADInitializationStatus * _Nonnull status) {
        NSDictionary *dic = status.adapterStatusesByClassName;
        NSLog(@"Google adapter init value is %@", dic);
    }];
    
    //开启通道
    //Applovin
    [ALPrivacySettings setHasUserConsent:YES];
    
    //Unity
    UADSMetaData *gdprConsentMetaData = [[UADSMetaData alloc] init];
    [gdprConsentMetaData set:@"gdpr.consent" value:@YES];
    [gdprConsentMetaData commit];

运行上面的代码之后,我们的项目基本已经集成了Google的广告中介,里面包含了applovin,unity,facebook三个广告平台。

通过pod 'GoogleMobileAdsMediationTestSuite',我们还集成了Google中介测试套件,可以通过测试套件来测试我们的项目是否已经正确的集成了Google广告中介,已经对应平台的中介适配器。

  1. 通过添加下面的代码来集成显示Google中介测试套件

UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    //此处需添加google平台所创建的对应的应用id
NSString *appId = @"your appId";
[GoogleMobileAdsMediationTestSuite presentWithAppID:appId onViewController:rootViewController delegate:nil];

此处需注意,中介套件使用完之后,需要移除此段代码。

相关文档:

中介测试套件:https://developers.google.com/admob/ios/mediation-test-suite

中介适配器applovin集成:https://developers.google.com/admob/ios/mediation/applovin

中介适配器unity集成:https://developers.google.com/admob/ios/mediation/unity

中介适配器facebook集成:https://developers.google.com/admob/ios/mediation/facebook

注意:ios的中介集成需要在上面的路径去寻找对应的中介适配器,在Google中介集成页面无法找到对应的入口。

上一篇下一篇

猜你喜欢

热点阅读