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