- 如果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下載 需要翻墻

里面有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é)一下必要的:
- 翻墻的網(wǎng)絡(luò)
- 版位編碼
- 添加測試賬號或管理員賬號
- 真機上安裝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 寫就可以

- 還有一點 可以分享給大家 ,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--;
}
}
參考鏈接:
- 官方集成文檔Audience Network
https://developers.facebook.com/docs/audience-network/
2.一位日本開發(fā)者 http://www.cl9.info/entry/2017/08/28/220000



