Skip to content

iOS 集成 Facebook 广告 Audience Network

iOS 集成 Facebook 广告 Audience Network

图片来源于网络

如果APP在海外一定用户基础后一定会开始想着变现,Facebook的 Audience Network是一个非常不错的选择。

Audience Network 由于墙的原因 国内几乎没什么攻略 ,遇到的坑在此记录一下。

官方集成文档Audience Network 需要翻墙

开始集成

·使用 Cocoapod 将以下代码行添加到项目的 Podfile 文件中:pod ‘FBAudienceNetwork’

运行 pod install 命令。

可是这第一步就出问题了

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

里面有SDK 和 Demo

我们把FBAudienceNetwork.framework 拖进工程

·在写代码之前 我们要在Facebook 后台查看一下 所创建广告版位的,这个版位是产品创建了,我们需要的是版位编码,

已我的理解 版位编码 是可以创建多个,不同显示类型的分别创建,比如Banner类型创建一个 ,Native类型创建一个。由于这是产品负责,以后有添加在补充。

·还有一步 是我们要添加测试账号

如果想要在你的测试机上显示AudienceNetwork. 广告,除了翻墙,并且要添加测试账号 ,还有你的测试机上安装Facebook应用 ,并使用管理员账号 或者 测试账号 登录Facebook ,否则无法请求成功。

总结一下必要的:

翻墙的网络

版位编码

添加测试账号或管理员账号

真机上安装Facebook 并使用测试账号或管理员登录

·接下来 就是 代码了 按照demo里写的就可以

因为我是在TableView中展示 所以用的是FBNativeAdsManager

代码如下

– (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 写就可以

·还有一点 可以分享给大家 ,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–;

      }

}

参考链接:

官方集成文档Audience Network

https://developers.facebook.com/docs/audience-network/

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

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

作者:大苏Andi

链接:https://www.jianshu.com/p/b0a9bac00d10

来源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

相关推荐: Shopify产品如何通过Facebook渠道销售?

您可以通过使用 Facebook 销售渠道在 Facebook 粉丝页展示 Shopify 产品。 当您设置 Facebook 销售渠道时,Facebook 会在您的 Facebook 粉丝页上创建商店部分来展示您的 Shopify 产品。您可以在 Shopi…

    码刀科技(www.lekshop.cn)是国内知名企业级电商平台提供商,为企业级商家提供最佳的电商平台搭建(多种模式电商平台搭建:B2B/B2B2C/B2C/O2O/新零售/跨境等)、平台管理系统开发及互联网采购解决方案服务, 联系客服了解更多.

    电子商务网站建设的重要性和好处