user2872856
user2872856

Reputation: 2051

Show Admob Native Ads Programmatically with Objective C

The official guide is using storyboard https://firebase.google.com/docs/admob/ios/native-express

I am trying to load the native ads in the header of UITableView without using storyboard.

What I have tried:

    @property (nonatomic, strong) GADNativeExpressAdView *nAdView;
...
- (void)viewDidLoad
{
    [super viewDidLoad];
        _nAdView.adUnitID = @"ca-app-pub-mycode";
        _nAdView.rootViewController = self;
}
...
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
        GADRequest *request = [GADRequest request];
        [self.nAdView loadRequest:request];
        return _nAdView;
}

But nothing happened.

Upvotes: 0

Views: 2036

Answers (1)

Toru
Toru

Reputation: 1264

Is your nAdView related to Storyboard? I think it is not. Maybe you have to correlate GADNativeExpressAdView instance to Storyboard. And then, you should load native ad like Google official site does:

@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

Edit:

Without storyboard, try this:

@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]];
}

Upvotes: 2

Related Questions