ios Facebook 添加 FBNativeAdView 作为子视图

ios Facebook add FBNativeAdView as Subview

我想使用来自 FBNativeAdView 的预建视图(不想自定义 FBNative 广告)。如 link

The FBNativeAdView creates prebuilt native ad template views and manages native ads.

我确实更改了 NativeAdSample 中给出的示例 Facebook SDK。并添加 FBNativeAdView 作为 mainView(adUIView) 的子视图。

-(void) nativeAdDidLoad: (FBNativeAd * ) nativeAd 
{
        NSLog(@"Native ad was loaded, constructing native UI...");

        if (self._nativeAd) 
        {
            [self._nativeAd unregisterView];
        }

        self._nativeAd = nativeAd;

        // Here I did add
        FBNativeAdViewAttributes * attributes = [[FBNativeAdViewAttributes alloc] init];
        attributes.backgroundColor = [UIColor whiteColor];
        attributes.titleColor = [UIColor blackColor];

        FBNativeAdView * fbNativeAdView = [FBNativeAdView nativeAdViewWithNativeAd: self._nativeAd withType: FBNativeAdViewTypeGenericHeight300 withAttributes: attributes];
}

所以问题是如何将 fbNativeAdView 添加为 ParentView 的子视图,以便它应该在父视图中查看 view.I 做到了

[self.adUIView addSubview:fbNativeAdView];

没有成功。

Native Ad Template给出了如何从FBNativeAd获取FBNativeAdView的信息。但是没有告诉如何在uiview中使用FBNativeAdView

NativeAdView 使用 FBMediaView 创建广告。您对 nativeAdView 300 的高度太低,无法加载任何类型的 FBMediaView。

如果您想使用 300 百的高度,请创建原生视图 (iOS) 并使用 fbNativeAd 返回的属性。例如在您的 nativeAdDidLoad 中执行此操作:

customView.titleLabel = nativeAd.title;
FFBAdImage *icon = nativeAd.icon;
[icon loadImageAsyncWithBlock:^(UIImage * _Nullable image) 
{
    [customView.imageView setImage:image]; //image returned by fb is 128x128
}];
customView.fbAdBtn.titleLabel.text = nativeAd.callToAction;

[self.view addSubView:customView];

如果你想让你的整个自定义视图都可以点击,那么到这个

[nativeAd registerViewForInteraction:customView withViewController:self]; 

如果您希望仅通过自定义视图中的按钮执行操作,请执行此操作。

NSArray *clikAble = [NSArray alloc] initWithObjects:customView.fbAdBtn];
[nativeAd registerViewForInteraction:customView withViewController:self withClickableViews:clikAble];

您可以根据需要使用许多其他属性。

并且不要忘记遵循以下准则:https://developers.facebook.com/docs/audience-network/guidelines/native-ads

并尝试将 FBNativeAdView 设为全屏大小,我认为这肯定会加载视图。

现在可以使用 FBNativeAdView 中的添加框架作为

fbNativeAdView.frame = CGRectMake(0, 0, 320, 120);

现在 Native Ad Template 还提供了有关如何在 uiview 中使用 FBNativeAdView 的信息。

The ad template can be customized by changing the values of its elements:

- (void)nativeAdDidLoad:(FBNativeAd *)nativeAd 
{
  FBNativeAdViewAttributes *attributes = [[FBNativeAdViewAttributes alloc] init];

  attributes.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
  attributes.buttonColor = [UIColor colorWithRed:0.4 green:0.9 blue:0.8 alpha:1];
  attributes.buttonTitleColor = [UIColor whiteColor];

  FBNativeAdView *adView = [FBNativeAdView nativeAdViewWithNativeAd:nativeAd 
      withType:FBNativeAdViewTypeGenericHeight300 withAttributes:attributes];

  [self.view addSubview:adView];

  CGSize size = self.view.bounds.size;
  CGFloat xOffset = size.width / 2 - 160;
  CGFloat yOffset = (size.height > size.width) ? 100 : 20;
  adView.frame = CGRectMake(xOffset, yOffset, 320, 300);

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