編程之家收集整理的這篇文章主要介紹了ios–FBAudienceNetwork:將FBNativeAd設(shè)置為FBMediaView堆疊UI,編程之家小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。
以下代碼行使我的UI堆棧
adMediaView.nativeAd = nativeAd
// adMediaView - FBMediaView
// nativeAd - FBNativeAd
我已經(jīng)嘗試將其在后臺線程中異步執(zhí)行,但沒有幫助.有辦法解決嗎?
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT,0),{
adMediaView.nativeAd = nativeAd
});
我已經(jīng)通過pod安裝了FBAudienceNetwork,并且更新了它.最新版本是4.7.0
pod 'FBAudienceNetwork'
解決方法
NativeAdView使用FBMediaView創(chuàng)建廣告.
現(xiàn)在,在您的View Controller頭文件中聲明了FBNativeAdDelegate協(xié)議,并聲明并將實(shí)例變量連接到您的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的實(shí)現(xiàn)文件中添加一個初始化FBNativeAd并請求加載廣告的方法:
FBNativeAd *nativeAd;
FBAdchoicesView *adChoicesView;
- (void)showNativeAd
{
nativeAd = [[FBNativeAd alloc] initWithPlacementID:@"YOUR_PLACEMENT_ID"];
nativeAd.delegate = self;
[nativeAd loadAd];
}
現(xiàn)在,您已經(jīng)添加了加載廣告的代碼,添加以下功能來處理加載故障,并在加載后構(gòu)建廣告:
- (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
總結(jié)
以上是編程之家為你收集整理的ios–FBAudienceNetwork:將FBNativeAd設(shè)置為FBMediaView堆疊UI全部內(nèi)容,希望文章能夠幫你解決ios–FBAudienceNetwork:將FBNativeAd設(shè)置為FBMediaView堆疊UI所遇到的程序開發(fā)問題。
如果覺得編程之家網(wǎng)站內(nèi)容還不錯,歡迎將編程之家網(wǎng)站推薦給程序員好友。