ios - FBAudienceNetwork : setting FBNativeAd into FBMediaView stacks the UI

标签 ios swift facebook-audience-network

下面这行代码使我的 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 协议(protocol)以及声明实例变量并将其连接到您的 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

关于ios - FBAudienceNetwork : setting FBNativeAd into FBMediaView stacks the UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34393963/

相关文章:

ios - UIImage(命名为 : string) is not assigned

swift - FBSDK 的 Settings Class 中没有找到 setAdvertiserTrackingEnabled?

ios - 在 UITableView 之上从 UIView 传递触摸事件

ios - Swift 向下转型和协议(protocol)变量

ios - 如何通过 Interface Builder 将 UIImage 对象放置在 UITableViewController View 的底部

ios - 将字符串数组打印到 UILabel

ios - NSDateFormatter : Using custom format with relative formatting

android - 我应该用什么替换已弃用的 Facebook InterstitialAd setAdListener?

android-studio - 通过 MoPub 的 Facebook Audience Network 中介(横幅和插页式广告)

ios - xcodebuild 错误 - SecKey API 返回 : -25308