下面这行代码使我的 UI 堆栈
adMediaView.nativeAd = nativeAd
// adMediaView - FBMediaView
// nativeAd - FBNativeAd
我试过将它放在后台线程中异步执行,但没有帮助。有办法解决吗?
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),{
adMediaView.nativeAd = nativeAd
});
我已经通过 pod 安装了
FBAudienceNetwork
并刚刚更新了它。我的最新版本是 4.7.0pod 'FBAudienceNetwork'
最佳答案
NativeAdView
使用 FBMediaView
创建广告。
现在,在您的 View Controller 头文件中声明 FBNativeAdDelegate 协议(protocol)以及声明实例变量并将其连接到您的 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
关于ios - FBAudienceNetwork : setting FBNativeAd into FBMediaView stacks the UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34393963/