iOS 集成 Facebook 廣告 Audience Network

  • 如果APP 在海外一定用戶基礎(chǔ)后一定會開始想著變現(xiàn) Facebook的 Audience Network是一個非常不錯的選擇
    Audience Network 由于墻的原因 國內(nèi)幾乎沒什么攻略 ,遇到的坑在此記錄一下
    官方集成文檔Audience Network 需要翻墻

開始集成

  • 使用 Cocoapod 將以下代碼行添加到項目的 Podfile 文件中:pod 'FBAudienceNetwork'
    運行 pod install 命令。
    可是這第一步就出問題了


    3606E67D-A527-4E9D-AA30-7A72D42AA534.png

我是翻墻的網(wǎng)絡(luò) 也下不了,各種查也沒辦法。所以改為手動集成Audience Network SDK下載 需要翻墻

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

里面有SDK 和 Demo
我們把FBAudienceNetwork.framework 拖進(jìn)工程

  • 在寫代碼之前 我們要在Facebook 后臺查看一下 所創(chuàng)建廣告版位的,這個版位是產(chǎn)品創(chuàng)建了,我們需要的是版位編碼,
    已我的理解 版位編碼 是可以創(chuàng)建多個,不同顯示類型的分別創(chuàng)建,比如Banner類型創(chuàng)建一個 ,Native類型創(chuàng)建一個。由于這是產(chǎn)品負(fù)責(zé),以后有添加在補充。


    D4088E78-F957-4E51-8EA2-6A722DCCF748.png
  • 還有一步 是我們要添加測試賬號
    如果想要在你的測試機上顯示AudienceNetwork. 廣告,除了翻墻,并且要添加測試賬號 ,還有你的測試機上安裝Facebook應(yīng)用 ,并使用管理員賬號 或者 測試賬號 登錄Facebook ,否則無法請求成功。


    0500238E-1C8B-4590-8E0B-5B903EECEC38.png

總結(jié)一下必要的:

  1. 翻墻的網(wǎng)絡(luò)
  2. 版位編碼
  3. 添加測試賬號或管理員賬號
  4. 真機上安裝Facebook 并使用測試賬號或管理員登錄
  • 接下來 就是 代碼了 按照demo里寫的就可以
    因為我是在TableView中展示 所以用的是FBNativeAdsManager
    代碼如下


    99B80182-B53C-4571-962A-6A8C01B9A733.png
- (void)loadNativeAd{
    if (!self.adsManager) {
      //第一個參數(shù)是剛才申請的版位編碼
      //第二個參數(shù) 請求廣告的個數(shù)
        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.");
}

現(xiàn)在就可以進(jìn)行調(diào)試了,結(jié)果我用iOS11的手機一直請求錯誤,下面是錯誤提示。

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

百度根本沒有,換谷歌 終于在一位日本開發(fā)者的博客中找到了解決方案:找一個iOS10手機 在設(shè)置頁面的Facebook里登錄并且安裝Facebook應(yīng)用,重新運行程序,終于看到了請求成功的提示

Native ad was loaded, constructing native UI...

后來我在另一個應(yīng)用中集成,iOS11 的手機直接就能請求成功。總結(jié)一句話:還得看運氣

  • UI的搭建,按照demo 寫就可以
96A20B86-6202-4004-B214-A4DE1796C171.png
  • 還有一點 可以分享給大家 ,F(xiàn)BNativeAd 是原生廣告的對象,包括了廣告的具體參數(shù),如果你是 在列表中展示廣告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;
    }
}

會發(fā)現(xiàn)很卡頓 官方的方法雖然 都是if 判斷,但某些場景下耦合度還是有點高,而且數(shù)據(jù)結(jié)構(gòu)復(fù)雜的頁面,經(jīng)常廣告和數(shù)據(jù)對不上。不想使用這個官方列表的方法,可以通過以下方法拿到原生廣告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(@"廣告?zhèn)€數(shù)count :%ld",count);
      while (count) {
            NSLog(@"廣告對象NativeAd :%@",manager.nextNativeAd);
        count--;
      }
}

參考鏈接:

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

2.一位日本開發(fā)者 http://www.cl9.info/entry/2017/08/28/220000

  1. swift https://disp.cc/b/11-aeBf
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,204評論 3 119
  • 上周,國內(nèi)院線殺出一匹黑馬:《大護(hù)法》 豆瓣8.1,坐實了目前今年國產(chǎn)電影口碑NO.1。 電影用風(fēng)格強烈的隱喻,道...
    獵影娘閱讀 385評論 0 1
  • 去醫(yī)院做身體檢查總是很累的事情。因為等待的時間太漫長了。 醫(yī)院這個地方什么時候去都是人滿為患。 各種不同的病癥,都...
    是個人類閱讀 352評論 0 0
  • 第一次去北京,其中的歡欣與激動不言于耳,然而在真正去了北京,才發(fā)現(xiàn)北京的環(huán)境,建筑以及人文其實都與我經(jīng)常去的西安是...
    塞飛夜閱讀 280評論 0 2
  • 和我相處,請帶上你的真誠 世界這么大,到處都是人。 不喜歡心眼太多的人, 因為玩不過人家; 不喜歡太有心機的人, ...
    crystal701閱讀 183評論 0 2

友情鏈接更多精彩內(nèi)容