使用Objective C

时间:2017-02-05 13:21:50

标签: ios objective-c admob

官方指南正在使用故事板 https://firebase.google.com/docs/admob/ios/native-express

我正在尝试在header的{​​{1}}中加载原生广告,而不使用故事板。

我尝试过:

UITableView

但什么都没发生。

1 个答案:

答案 0 :(得分:2)

您的nAdView与故事板有关吗?我认为不是。 也许您必须将GADNativeExpressAdView实例与Storyboard相关联。 然后,您应该像Google官方网站一样加载原生广告:

@import GoogleMobileAds;

@interface ViewController ()

@property(nonatomic, weak) IBOutlet GADNativeExpressAdView *nativeExpressAdView;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  self.nativeExpressAdView.adUnitID = @"ca-app-pub-3940256099942544/2562852117";
  self.nativeExpressAdView.rootViewController = self;

  GADRequest *request = [GADRequest request];
  [self.nativeExpressAdView loadRequest:request];
}

@end

修改

如果没有故事板,请尝试以下方法:

@property (nonatomic, strong) GADNativeExpressAdView *nAdView;

- (void)ViewDidLoad {
  self.nAdView = [[GADNativeExpressAdView alloc] initWithAdSize:GADAdSizeFromCGSize(CGSizeMake(width, GADAdViewHeight))];
  self.nAdView.adUnitID = @"ca-app-pub-mycode";
  self.nAdView.rootViewController = self;
  self.nAdView.delegate = self;
  [self.nAdView loadRequest:[GADRequest request]];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  return self.nAdView;
}

- (void)nativeExpressAdView:(GADNativeExpressAdView *)nativeExpressAdView
    didFailToReceiveAdWithError:(GADRequestError *)error {
  NSLog(@"Failed to receive ad: %@", error.localizedDescription);
  [self.nAdView loadRequest:[GADRequest request]];
}