ios Facebook add FBNativeAdView as Subview
Asked Answered
U

2

8

I want to use prebuilt view from FBNativeAdView(don't want to customize FBNative Ad).As given in the link

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

And I did Changed NativeAdSample example given in Facebook SDK.And add FBNativeAdView as a subview of 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];
}

So the question is how to add fbNativeAdView as a subview of ParentView so it should view in parent view.I did it

[self.adUIView addSubview:fbNativeAdView];

with no success.

Native Ad Template gives information about how to get FBNativeAdView from FBNativeAd.But didn't told about how to use FBNativeAdView in uiview.

Unicuspid answered 4/9, 2015 at 6:34 Comment(6)
"With no success" --> what happened? Any errors/notices?Penicillate
Only White background view.Unicuspid
Same for me... just background (for which I can set the color) but no other content. The examples are not even using the FBNativeAdViewColemancolemanite
I don't know, but don't you have to set the frame of the fbadview ? The background color you see is of the fbadview or is it of the content view?Tachistoscope
@Penicillate did you check,because I didn't find suitable answer,till now I am using FBNativeAdScrollView with one ad request and it's working fine,but I want to get success with FBNativeAdView.Unicuspid
@Penicillate please look at #35747143Unicuspid
U
1

Now It work using add frame in FBNativeAdView as

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

Also now Native Ad Template gives information about how to use FBNativeAdView in uiview.

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];
}
Unicuspid answered 9/3, 2016 at 9:56 Comment(0)
P
-2

NativeAdView uses FBMediaView for creating an ad. Your hight for nativeAdView 300, is too low for any kind of FBMediaView to load.

If you want to use hight of 300 hundred, create a native view(iOS) and use properties returned by fbNativeAd. e.g. do this in your 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];

if you want your whole custom view to be clickable then to this

[nativeAd registerViewForInteraction:customView withViewController:self]; 

if you want action to be taken only by button in your custom view then do this.

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

There are number of other properties you can use according to your need.

And don't forget to follow this guidelines : https://developers.facebook.com/docs/audience-network/guidelines/native-ads

And try to make full screen size FBNativeAdView i think that will definitely load a view.

Polyvalent answered 16/11, 2015 at 12:28 Comment(5)
That was the solution dude - Your hight for nativeAdView 300, is too low for any kind of FBMediaView to load.Bookkeeping
Wait so you are saying that the defaults provided by Facebook are too low to be useful, e.g. FBNativeAdViewTypeGenericHeight100, FBNativeAdViewTypeGenericHeight300Cloison
@XCodeWarrier they are too low for FBMediaView.Polyvalent
@ibnetariq: that does not make sense. If it's too low the FBMediaView should not appear but the title, button, etc should be there. Like in the screenshots in Facebook guidelines: developers.facebook.com/docs/audience-network/ios/…Colemancolemanite
@Polyvalent you said "NativeAdView uses FBMediaView for creating an ad. Your hight for nativeAdView 300, is too low for any kind of FBMediaView to load." then why FB uses height FBNativeAdViewTypeGenericHeight300?Anyway this is wrong answer.Unicuspid

© 2022 - 2024 — McMap. All rights reserved.