Ios Facebook add FBNativeAdView as Subview

I want to use a pre-built view from FBNativeAdView (I do not want to configure FBNative Ad). As indicated in the link

FBNativeAdView creates pre-created custom ad template templates and manages its own ads.

And I did the NativeAdSample example provided in the Facebook SDK . And add the FBNativeAdView as the MainView subtask (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 the FBNativeAdView as a subset of the ParentView so that it looks in the parent view. I did it

 [self.adUIView addSubview:fbNativeAdView]; 

without success.

The native ad template provides information on how to get the FBNativeAdView from FBNativeAd . But did not talk about how to use FBNativeAdView in uiview.

+8
ios facebook facebook-audience-network
source share
2 answers

Now it works by adding a frame to the FBNativeAdView as

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

Also now, the Native Ad Template provides information on how to use the FBNativeAdView in uiview.

An 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]; } 
0
source share

NativeAdView uses FBMediaView to create an ad. Your rating for nativeAdView 300 is too low to load any type of FBMediaView.

If you want to use a height of 300 hundred, create your own view (iOS) and use the properties returned by fbNativeAd. for example 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 the whole custom view to be clickable and then

 [nativeAd registerViewForInteraction:customView withViewController:self]; 

if you want the action to be performed only by a button in your user view, do it.

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

There are a number of other properties that you can use to suit your needs.

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

And try to make the full screen size FBNativeAdView, I think it will definitely load the view.

-2
source share

All Articles