NativeAdView
uses FBMediaView
for creating an ad.
Now, in your View Controller header file declare FBNativeAdDelegate protocol as well as declare and connect instance variables to your 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
Then, add a method in your View Controller's implementation file that initializes FBNativeAd
and request an ad to load:
FBNativeAd *nativeAd;
FBAdchoicesView *adChoicesView;
- (void)showNativeAd
{
nativeAd = [[FBNativeAd alloc] initWithPlacementID:@"YOUR_PLACEMENT_ID"];
nativeAd.delegate = self;
[nativeAd loadAd];
}
Now that you have added the code to load the ad, add the following functions to handle loading failures and to construct the ad once it has loaded:
- (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);
}
For displaying the native ad cover image, it is recommended to use the Facebook Audience Network MediaView which can display both image and video assets.
Reference : https://developers.facebook.com/docs/audience-network/ios/native-api