ios – FBAudienceNetwork:将FBNativeAd设置为FBMediaView堆叠UI

以下代码行使我的UI堆栈
adMediaView.nativeAd = nativeAd 
// adMediaView - FBMediaView
// nativeAd - FBNativeAd

我已经尝试将其在后台线程中异步执行,但没有帮助.有办法解决吗?

dispatch_async(dispatch_get_global_queue( disPATCH_QUEUE_PRIORITY_DEFAULT,0),{
    adMediaView.nativeAd = nativeAd 
});

>我已经通过pod安装了FBAudienceNetwork,并且更新了它.最新版本是4.7.0

pod 'FBAudienceNetwork'

解决方法

NativeAdView使用FBMediaView创建广告.
现在,在您的View Controller头文件中声明了FBNativeAdDelegate协议,并声明并将实例变量连接到您的UI .XIB:
@import FBAudienceNetwork; // import Audience Network module

@interface MyViewController : UIViewController <FBNativeAdDelegate>
  // Other code might go here...
  @property (weak,nonatomic) IBOutlet UIImageView *adIconImageView;
  @property (weak,nonatomic) IBOutlet UILabel *adTitleLabel;
  @property (weak,nonatomic) IBOutlet UILabel *adBodyLabel;
  @property (weak,nonatomic) IBOutlet UIButton *adCallToActionButton;
  @property (weak,nonatomic) IBOutlet UILabel *adSocialContextLabel;
  @property (weak,nonatomic) IBOutlet UILabel *sponsoredLabel;

  @property (weak,nonatomic) FBMediaView *adCoverMediaView;

  @property (weak,nonatomic) IBOutlet UIView *adView;
@end

然后,在View Controller的实现文件添加一个初始化FBNativeAd并请求加载广告的方法

FBNativeAd *nativeAd;
FBAdchoicesView *adChoicesView;

- (void)showNativeAd
{
  nativeAd = [[FBNativeAd alloc] initWithPlacementID:@"YOUR_PLACEMENT_ID"];
  nativeAd.delegate = self;
  [nativeAd loadAd];
}

现在,您已经添加了加载广告的代码,添加以下功能来处理加载故障,并在加载后构建广告:

- (void)nativeAdDidLoad:(FBNativeAd *)nativeAd
{
  [self.adTitleLabel setText:nativeAd.title];
  [self.adBodyLabel setText:nativeAd.body];
  [self.socialContextLabel setText:nativeAd.socialContext];
  [self.sponsoredLabel setText:@”Sponsored”];
  [self.adCallToActionButton setTitle:nativeAd.callToAction];

  [nativeAd.icon loadImageAsyncWithBlock:^(UIImage *image) {
    [self.adIconImageView setimage:image];      
  }];

  // Allocate a FBMediaView to contain the cover image or native video asset
  adCoverMediaView = [[FBMediaView alloc] initWithFrame:coverFrame]];
  [adCoverMediaView setNativeAd:nativeAd];

  // Add adChoicesView
  adChoicesView = [[FBAdChoicesView alloc] initWithNativeAd:nativeAd];
  [adView addSubview:adChoicesView];
  [adChoicesView updateFrameFromSuperview];

  // Register the native ad view and its view controller with the native ad instance
  [nativeAd registerViewForInteraction:adView withViewController:self];
}

- (void)nativeAd:(FBNativeAd *)nativeAd didFailWithError:(NSError *)error
{
  NSLog(@"Ad Failed to load with error: %@",error);
}

显示原生广告封面图片,建议您使用能够同时显示图片和影片资源的Facebook Audience Network MediaView.

参考:https://developers.facebook.com/docs/audience-network/ios/native-api

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...