Facebook 原生广告 FBNativeAdsManagerDelegate 实现方法没有调用

Posted

技术标签:

【中文标题】Facebook 原生广告 FBNativeAdsManagerDelegate 实现方法没有调用【英文标题】:Facebook Native Ads FBNativeAdsManagerDelegate implementation methods did not called 【发布时间】:2016-03-02 12:01:41 【问题描述】:

Facebook Native Ads 中的FBNativeAdsManagerDelegateUIViewController 类中正常工作,但在自定义 NSObject 类中使用时它不起作用,即它的委托方法 nativeAdsLoadednativeAdsFailedToLoadWithError 没有被调用。

CustomFBAd.h 文件

@import FBAudienceNetwork;

#import <Foundation/Foundation.h>

@protocol OnFBNativeAdLoadedDelegate<NSObject>

- (void)onFBNativeAdLoaded:(UIView *)adView;

@end

@interface CustomFBAd : NSObject

@property (nonatomic,weak) id <OnFBNativeAdLoadedDelegate>delegate;

-(void)requestNativeAd:(NSString *)FaceBookPlacementID;
@end

CustomFBAd.m 文件

#import "CustomFBAd.h"

@interface CustomFBAd ()<FBNativeAdsManagerDelegate,FBNativeAdDelegate>

@property (nonatomic, strong) FBNativeAdsManager *manager;
@property (nonatomic, weak) FBNativeAdScrollView *scrollView;

@end
@implementation CustomFBAd

-(void)requestNativeAd:(NSString *)FaceBookPlacementID
    if(FaceBookPlacementID.length != 0)
        FBNativeAdsManager *manager = [[FBNativeAdsManager alloc] initWithPlacementID:FaceBookPlacementID forNumAdsRequested:5];
        manager.delegate = self;
        [FBAdSettings addTestDevice:@"cf1bb93becbe6e31f26fdf7d80d19b4ae225afaa"];
        [manager loadAds];
        self.manager = manager;
    


#pragma mark - FBNativeAdDelegate implementation

- (void)nativeAdDidClick:(FBNativeAd *)nativeAd

    //    NSLog(@"Native ad was clicked.");


- (void)nativeAdDidFinishHandlingClick:(FBNativeAd *)nativeAd

    //    NSLog(@"Native ad did finish click handling.");


- (void)nativeAdWillLogImpression:(FBNativeAd *)nativeAd

    //    NSLog(@"Native ad impression is being captured.");


#pragma mark FBNativeAdsManagerDelegate

-(void)nativeAdDidLoad:(FBNativeAd *)nativeAd




- (void)nativeAdsLoaded

    NSLog(@"Native ads loaded, constructing native UI...");

    if (self.scrollView) 
        [self.scrollView removeFromSuperview];
        self.scrollView = nil;
    

    FBNativeAdScrollView *scrollView = [[FBNativeAdScrollView alloc] initWithNativeAdsManager:self.manager withType:FBNativeAdViewTypeGenericHeight120];
    scrollView.xInset = 0;
    scrollView.delegate = self;
    self.scrollView = scrollView;

    [self.delegate onFBNativeAdLoaded:self.scrollView];


- (void)nativeAdsFailedToLoadWithError:(NSError *)error

    NSLog(@"Native ads failed to load with error: %@", error);


@end

如上述代码中所述,我确实在requestNativeAd 方法中将 FBNativeAdsManager 的委托设置为

manager.delegate = self;

也用作FBNativeAdsManagerDelegate,FBNativeAdDelegate

@interface CustomFBAd ()<FBNativeAdsManagerDelegate,FBNativeAdDelegate>

并将此代码称为

    CustomFBAd *objFBAd = [[CustomFBAd alloc]init];
    objFBAd.delegate = self;
    [objFBAd requestNativeAd:@"my_FB_placement_Id"];

任何线索(注意:如果我在UIViewController 中使用相同的代码)?谢谢

【问题讨论】:

两天后还有 Facebook 的人可以回答吗? 如果您的委托方法在 uiviewcontroller 中被调用,则代码存在问题。我想你必须在你的控制器中有一个强大的 CustomFBAd 参考。因为没有其他参考资料占用您的 CustomFBAd。希望它有所帮助:) 【参考方案1】:

在强烈引用CustomFBAd 之后它终于起作用了,它就像一个魅力(感谢@MuhammadZohaibEhsan)。所以初始化CustomFBAd as

@property(nonatomic, strong) CustomFBAd * objFBAd;

然后改变

    CustomFBAd *objFBAd = [[CustomFBAd alloc]init];
    objFBAd.delegate = self;
    [objFBAd requestNativeAd:@"my_FB_placement_Id"];

    self.objFBAd = [[CustomFBAd alloc]init];
    self.objFBAd.delegate = self;
    [self.objFBAd requestNativeAd:@"my_FB_placement_Id"];

【讨论】:

我可以发布这个作为答案吗:) 终于救了我。谢谢你:)【参考方案2】:

如果您的委托方法在 uiviewcontroller 中被调用,则代码存在问题。我想你必须在你的控制器中有一个强大的 CustomFBAd 参考。因为没有其他参考资料占用您的 CustomFBAd。希望对你有帮助

【讨论】:

终于救了我。谢谢你:)

以上是关于Facebook 原生广告 FBNativeAdsManagerDelegate 实现方法没有调用的主要内容,如果未能解决你的问题,请参考以下文章

在 ListView 中显示重复布局的 Facebook 原生横幅广告

原生 Facebook Audience Network 广告不可点击

GridView 中的 Facebook 原生广告:MediaView 显示灰色矩形

RycyclerView android 中的 Facebook 原生广告

Facebook 原生广告 API,仅显示视频

为 Facebook 的 Android 原生广告预取图像