iOS 开发每天分享优质文章ios实用开发技巧iOS开发

iOS 集成 Facebook 广告 Audience Net

2018-07-06  本文已影响18人  _Andy_

开始集成

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

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

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

总结一下必要的:

  1. 翻墙的网络
  2. 版位编码
  3. 添加测试账号或管理员账号
  4. 真机上安装Facebook 并使用测试账号或管理员登录
- (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 的手机直接就能请求成功。总结一句话:还得看运气

96A20B86-6202-4004-B214-A4DE1796C171.png
#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 :%ld",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
上一篇 下一篇

猜你喜欢

热点阅读