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

前端之家收集整理的这篇文章主要介绍了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

原文链接:https://www.f2er.com/iOS/335493.html

猜你在找的iOS相关文章