微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/iOS/335493.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐