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

前端之家收集整理的这篇文章主要介绍了ios – Facebook原生广告没有调用FBNativeAdsManagerDelegate实现方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Facebook Native Ads中的FBNativeAdsManagerDelegate在UIViewController类中正常工作,但在自定义NSObject类中使用时,它无法正常工作,即其委托方法nativeAdsLoaded和nativeAds@R_301_159@ToLoadWithError未被调用.

CustomFBAd.h文件

  1. @import FBAudienceNetwork;
  2.  
  3. #import <Foundation/Foundation.h>
  4.  
  5. @protocol OnFBNativeAdLoadedDelegate<NSObject>
  6.  
  7. - (void)onFBNativeAdLoaded:(UIView *)adView;
  8.  
  9. @end
  10.  
  11. @interface CustomFBAd : NSObject
  12.  
  13. @property (nonatomic,weak) id <OnFBNativeAdLoadedDelegate>delegate;
  14.  
  15. -(void)requestNativeAd:(NSString *)FaceBookPlacementID;
  16. @end

CustomFBAd.m文件

  1. #import "CustomFBAd.h"
  2.  
  3. @interface CustomFBAd ()<FBNativeAdsManagerDelegate,FBNativeAdDelegate>
  4.  
  5. @property (nonatomic,strong) FBNativeAdsManager *manager;
  6. @property (nonatomic,weak) FBNativeAdScrollView *scrollView;
  7.  
  8. @end
  9. @implementation CustomFBAd
  10.  
  11. -(void)requestNativeAd:(NSString *)FaceBookPlacementID{
  12. if(FaceBookPlacementID.length != 0){
  13. FBNativeAdsManager *manager = [[FBNativeAdsManager alloc] initWithPlacementID:FaceBookPlacementID forNumAdsRequested:5];
  14. manager.delegate = self;
  15. [FBAdSettings addTestDevice:@"cf1bb93becbe6e31f26fdf7d80d19b4ae225afaa"];
  16. [manager loadAds];
  17. self.manager = manager;
  18. }
  19. }
  20.  
  21. #pragma mark - FBNativeAdDelegate implementation
  22.  
  23. - (void)nativeAdDidClick:(FBNativeAd *)nativeAd
  24. {
  25. // NSLog(@"Native ad was clicked.");
  26. }
  27.  
  28. - (void)nativeAdDidFinishHandlingClick:(FBNativeAd *)nativeAd
  29. {
  30. // NSLog(@"Native ad did finish click handling.");
  31. }
  32.  
  33. - (void)nativeAdWillLogImpression:(FBNativeAd *)nativeAd
  34. {
  35. // NSLog(@"Native ad impression is being captured.");
  36. }
  37.  
  38. #pragma mark FBNativeAdsManagerDelegate
  39.  
  40. -(void)nativeAdDidLoad:(FBNativeAd *)nativeAd
  41. {
  42.  
  43. }
  44.  
  45. - (void)nativeAdsLoaded
  46. {
  47. NSLog(@"Native ads loaded,constructing native UI...");
  48.  
  49. if (self.scrollView) {
  50. [self.scrollView removeFromSuperview];
  51. self.scrollView = nil;
  52. }
  53.  
  54. FBNativeAdScrollView *scrollView = [[FBNativeAdScrollView alloc] initWithNativeAdsManager:self.manager withType:FBNativeAdViewTypeGenericHeight120];
  55. scrollView.xInset = 0;
  56. scrollView.delegate = self;
  57. self.scrollView = scrollView;
  58.  
  59. [self.delegate onFBNativeAdLoaded:self.scrollView];
  60. }
  61.  
  62. - (void)nativeAds@R_301_159@ToLoadWithError:(NSError *)error
  63. {
  64. NSLog(@"Native ads @R_301_159@ to load with error: %@",error);
  65. }
  66.  
  67. @end

如上面的代码所述,我在requestNativeAd方法中设置了FBNativeAdsManager的委托

  1. manager.delegate = self;

并且还用作FBNativeAdsManagerDelegate,FBNativeAdDelegate

  1. @interface CustomFBAd ()<FBNativeAdsManagerDelegate,FBNativeAdDelegate>

并将此代码称为

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

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

解决方法

如果你的委托方法是在uiviewcontroller中调用的,那么代码就会出现问题.我想你必须在你的控制器中有一个强大的CustomFBAd参考.因为其他参考文献都没有抓住您的CustomFBAd.希望能帮助到你

猜你在找的iOS相关文章