FBNativeAdsManagerDelegate
in Facebook Native Ads working properly in UIViewController
class but when used in custom NSObject class its not working i.e. its delegate methods nativeAdsLoaded
and nativeAdsFailedToLoadWithError
did not get called.
CustomFBAd.h file
@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 file
#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
As stated in above code I did set FBNativeAdsManager's delegate in requestNativeAd
method as
manager.delegate = self;
And also used as FBNativeAdsManagerDelegate,FBNativeAdDelegate
@interface CustomFBAd ()<FBNativeAdsManagerDelegate,FBNativeAdDelegate>
And call this code as
CustomFBAd *objFBAd = [[CustomFBAd alloc]init];
objFBAd.delegate = self;
[objFBAd requestNativeAd:@"my_FB_placement_Id"];
any clue (Note : same code works if I use it in UIViewController
)? Thanks
Finally it works after making strong reference of CustomFBAd
it works like a charm(Thanks to @MuhammadZohaibEhsan).So init CustomFBAd
as
@property(nonatomic, strong) CustomFBAd * objFBAd;
And change
CustomFBAd *objFBAd = [[CustomFBAd alloc]init];
objFBAd.delegate = self;
[objFBAd requestNativeAd:@"my_FB_placement_Id"];
to
self.objFBAd = [[CustomFBAd alloc]init];
self.objFBAd.delegate = self;
[self.objFBAd requestNativeAd:@"my_FB_placement_Id"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With