iOS 集成 Facebook 广告 Audience Network

  • 如果APP 在海外一定用户基础后一定会开始想着变现 Facebook的 Audience Network是一个非常不错的选择
    Audience Network 由于墙的原因 国内几乎没什么攻略 ,遇到的坑在此记录一下
    官方集成文档Audience Network 需要翻墙

开始集成

  • 使用 Cocoapod 将以下代码行添加到项目的 Podfile 文件中:pod 'FBAudienceNetwork'
    运行 pod install 命令。
    可是这第一步就出问题了


    iOS 集成 Facebook 广告 Audience Network_第1张图片
    3606E67D-A527-4E9D-AA30-7A72D42AA534.png

我是翻墙的网络 也下不了,各种查也没办法。所以改为手动集成Audience Network SDK下载 需要翻墙

113E02F2-5B49-4685-BA2F-1475AF59A166.png

里面有SDK 和 Demo
我们把FBAudienceNetwork.framework 拖进工程

  • 在写代码之前 我们要在Facebook 后台查看一下 所创建广告版位的,这个版位是产品创建了,我们需要的是版位编码,
    已我的理解 版位编码 是可以创建多个,不同显示类型的分别创建,比如Banner类型创建一个 ,Native类型创建一个。由于这是产品负责,以后有添加在补充。


    iOS 集成 Facebook 广告 Audience Network_第2张图片
    D4088E78-F957-4E51-8EA2-6A722DCCF748.png
  • 还有一步 是我们要添加测试账号
    如果想要在你的测试机上显示AudienceNetwork. 广告,除了翻墙,并且要添加测试账号 ,还有你的测试机上安装Facebook应用 ,并使用管理员账号 或者 测试账号 登录Facebook ,否则无法请求成功。


    iOS 集成 Facebook 广告 Audience Network_第3张图片
    0500238E-1C8B-4590-8E0B-5B903EECEC38.png

总结一下必要的:

  1. 翻墙的网络
  2. 版位编码
  3. 添加测试账号或管理员账号
  4. 真机上安装Facebook 并使用测试账号或管理员登录
  • 接下来 就是 代码了 按照demo里写的就可以
    因为我是在TableView中展示 所以用的是FBNativeAdsManager
    代码如下


    iOS 集成 Facebook 广告 Audience Network_第4张图片
    99B80182-B53C-4571-962A-6A8C01B9A733.png
- (void)loadNativeAd{
    if (!self.adsManager) {
      //第一个参数是刚才申请的版位编码
      //第二个参数 请求广告的个数
        self.adsManager = [[FBNativeAdsManager alloc] initWithPlacementID:@"YOUR_PLACEMENT_ID"
                                                        forNumAdsRequested:5];
        // Set a delegate to get notified when the ads are loaded.
        self.adsManager.delegate = self;
        // Configure native ad manager to wait to call nativeAdsLoaded until all ad assets are loaded
        self.adsManager.mediaCachePolicy = FBNativeAdsCachePolicyAll;
    }
    [self.adsManager loadAds];
}
#pragma mark FBNativeAdsManagerDelegate implementation
- (void)nativeAdsLoaded{
    NSLog(@"Native ad was loaded, constructing native UI...");
}
- (void)nativeAdsFailedToLoadWithError:(NSError *)error{
    NSLog(@"Native ad failed to load with error: %@", error);
}
#pragma mark FBNativeAdDelegate
- (void)nativeAdDidClick:(FBNativeAd *)nativeAd{
    NSLog(@"Native ad was clicked.");
}
- (void)nativeAdDidFinishHandlingClick:(FBNativeAd *)nativeAd{
    NSLog(@"Native ad did finish click handling.");
}

- (void)nativeAdWillLogImpression:(FBNativeAd *)nativeAd{
    NSLog(@"Native ad impression is being captured.");
}

现在就可以进行调试了,结果我用iOS11的手机一直请求错误,下面是错误提示。

Initial request from a bundle must come from a App Admin, Developer or Tester

百度根本没有,换谷歌 终于在一位日本开发者的博客中找到了解决方案:找一个iOS10手机 在设置页面的Facebook里登录并且安装Facebook应用,重新运行程序,终于看到了请求成功的提示

Native ad was loaded, constructing native UI...

后来我在另一个应用中集成,iOS11 的手机直接就能请求成功。总结一句话:还得看运气

  • UI的搭建,按照demo 写就可以
iOS 集成 Facebook 广告 Audience Network_第5张图片
96A20B86-6202-4004-B214-A4DE1796C171.png
  • 还有一点 可以分享给大家 ,FBNativeAd 是原生广告的对象,包括了广告的具体参数,如果你是 在列表中展示广告TableView或CollectionView,
    使用了 FBNativeAdsManager 请求多个原生广告 如TableView使用了 FBNativeAdTableViewCellProvider
#pragma mark FBNativeAdsManagerDelegate implementation

- (void)nativeAdsLoaded
{
    NSLog(@"Native ad was loaded, constructing native UI...");

    // After the native ads have loaded we create the native ad cell provider and let it take over
    FBNativeAdsManager *manager = self.adsManager;
    self.adsManager.delegate = nil;
    self.adsManager = nil;
    // The native ad cell provider operates over a loaded ads manager and can create table cells with native
    // ad templates in them as well as help with the math to have a consistent distribution of ads within a table.
    FBNativeAdTableViewCellProvider *cellProvider = [[FBNativeAdTableViewCellProvider alloc] initWithManager:manager forType:FBNativeAdViewTypeGenericHeight300];
    self.cellProvider = cellProvider;
    self.cellProvider.delegate = self;


    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // In this example the ads are evenly distributed within the table every kRowStrideForAdCell-th cell.
    NSUInteger count = [self.tableViewContentArray count];
    count = [self.cellProvider adjustCount:count forStride:kRowStrideForAdCell] ?: count;
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // For ad cells just as the ad cell provider, for normal cells do whatever you would do.
    if ([self.cellProvider isAdCellAtIndexPath:indexPath forStride:kRowStrideForAdCell]) {
        return [self.cellProvider tableView:tableView cellForRowAtIndexPath:indexPath];
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kDefaultCellIdentifier forIndexPath:indexPath];
        // In this example we need to adjust the index back to the domain of the data.
        indexPath = [self.cellProvider adjustNonAdCellIndexPath:indexPath forStride:kRowStrideForAdCell] ?: indexPath;
        cell.textLabel.text = [self.tableViewContentArray objectAtIndex:indexPath.row];
        return cell;
    }
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // The ad cell provider knows the height of ad cells based on its configuration
    if ([self.cellProvider isAdCellAtIndexPath:indexPath forStride:kRowStrideForAdCell]) {
        return [self.cellProvider tableView:tableView heightForRowAtIndexPath:indexPath];
    } else {
        return 80;
    }
}

会发现很卡顿 官方的方法虽然 都是if 判断,但某些场景下耦合度还是有点高,而且数据结构复杂的页面,经常广告和数据对不上。不想使用这个官方列表的方法,可以通过以下方法拿到原生广告FBNativeAd 对象 自行搭建列表

#pragma mark FBNativeAdsManagerDelegate implementation
- (void)nativeAdsLoaded
{
    APDLog(@"Native ad was loaded, constructing native UI...");
    
    FBNativeAdsManager *manager = self.adsManager;
    self.adsManager.delegate = nil;
    self.adsManager = nil;
    NSUInteger count =manager.uniqueNativeAdCount;
    NSLog(@"广告个数count :%ld",count);
      while (count) {
            NSLog(@"广告对象NativeAd :%@",manager.nextNativeAd);
        count--;
      }
}

参考链接:

  1. 官方集成文档Audience Network
    https://developers.facebook.com/docs/audience-network/

2.一位日本开发者 http://www.cl9.info/entry/2017/08/28/220000

  1. swift https://disp.cc/b/11-aeBf

你可能感兴趣的:(iOS 集成 Facebook 广告 Audience Network)