FBNativeAdsManagerDelegate
在UIViewController
类中正常运行,但在自定义NSObject类中使用时,它无效,即其委托方法nativeAdsLoaded
和nativeAdsFailedToLoadWithError
未被调用
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
中使用它,相同的代码是否有效)?谢谢
答案 0 :(得分:3)
最后,在强烈引用CustomFBAd
之后,它的作用就像一个魅力(感谢@MuhammadZohaibEhsan)。所以初始CustomFBAd
为
@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"];
答案 1 :(得分:0)
如果在uiviewcontroller中调用了委托方法,那么代码就会出现问题。我想你必须在你的控制器中有一个强大的CustomFBAd参考。因为其他参考文献都没有抓住您的CustomFBAd。希望它有所帮助