11

以下代码行使我的 UI 堆栈

adMediaView.nativeAd = nativeAd 
// adMediaView - FBMediaView
// nativeAd - FBNativeAd

我试过把它放在后台线程中异步执行它,但它没有帮助。有没有办法解决它?

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),{
    adMediaView.nativeAd = nativeAd 
});
  • 我已经FBAudienceNetwork通过 pod 安装并刚刚更新了它。我的最新版本是 4.7.0

    pod 'FBAudienceNetwork' 
    
4

1 回答 1

-1

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

于 2015-12-29T20:38:53.577 回答