Gir*_*hai 8 facebook objective-c ios facebook-audience-network
FBNativeAdsManagerDelegate在Facebook Native Ads在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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
如上面的代码所述,我确实在方法中设置了FBNativeAdsManager的委托requestNativeAd
manager.delegate = self;
Run Code Online (Sandbox Code Playgroud)
并且还用作FBNativeAdsManagerDelegate,FBNativeAdDelegate
@interface CustomFBAd ()<FBNativeAdsManagerDelegate,FBNativeAdDelegate>
Run Code Online (Sandbox Code Playgroud)
并将此代码称为
CustomFBAd *objFBAd = [[CustomFBAd alloc]init];
objFBAd.delegate = self;
[objFBAd requestNativeAd:@"my_FB_placement_Id"];
Run Code Online (Sandbox Code Playgroud)
任何线索(注意:如果我使用它,相同的代码可以工作UIViewController)?谢谢
如果您的委托方法在 uiviewcontroller 中调用,则代码存在问题。我想你的控制器中必须有 CustomFBAd 的强大引用。因为没有其他引用可以控制您的 CustomFBAd。希望能帮助到你