AFS Native Implementation for iOS

The Google Mobile Ads SDK also supports custom search styles. If your app already uses the Google Mobile Ads SDK, we recommend you use the AFSMA SDK version instead.

Prerequisites

  • Use Xcode 8.0 or higher
  • Target iOS 8.0 or higher
  • A valid publisher web property code (e.g. vert-ppa-test1-srp)
  • CocoaPods

Import the AFS Native SDK

CocoaPods

Import the Google-AFSNative SDK into an iOS project using CocoaPods. Open your project's Podfile and add this line to your app's target:

pod 'Google-AFSNative'

Then from the command line run:

pod install --repo-update

If you're new to CocoaPods, see their official documentation for info on how to create and use Podfiles.

Overview

If you're upgrading to version 4.0 or later from 2.0.8 or earlier, please see our migration guide.

This document outlines the process to integrate AFS Native ads in your iOS mobile app.

GANSearchAdController

  • The GANSearchAdController constructor needs to be provided the publisher's web property code, desired settings ID and associated GANSearchAdControllerOptions object.
  • Each call to loadAds() indicates a new search, and it will cause the current set of ads to be discarded and invalidated.
  • Ad creatives are stored in GANAdView.
  • Ads are inserted into the ad GANAdView with the populateAdView method. In addition to the GANAdView which is to be populated, the caller provides an adIdentifier, which is an arbitrary string that should uniquely identify the ad. Inside the API, a specific ad is assigned to each adIdentifier that is passed in. Then, whenever that adKey is passed again in the future, the same ad will be returned. For example, if populateAdView is called for the first time with adIdentifier "keyA", each subsequent call to populateAdView while passing "keyA" as the adIdentifier will result in the same ad being shown.

GANAdView

  • This is the UIView that contains that the ad creative.
  • Populate this view with an ad using the populateAdView method on the GANSearchAdController.

GANSearchAdControllerOptions

  • Pass this object to the GANSearchAdController constructor to specify the behavior of how ads are requested and displayed.

GANSearchAdRequest

  • Call the loadAds method on the GANSearchAdController instance with this object to make an ad request.

GANSearchAdControllerDelegate

  • Implement this interface and provide it to the GANSearchAdController to register callbacks for several states.

Example implementation

The example below demonstrates creating a GANSearchAdController and a GANView to show an ad in a sample ViewController.

// SampleAppViewController.m implementation

#import <AFSNative/AFSNative.h>

@interface GBannerViewController () {
  // The Ad Controller used by the sample application.
  GANSearchAdController *_adController;

  // The Ad View to display the loaded ad.
  GANAdView *_adView;
}
// scrollView will be where we place our ads in this example.
@property(nonatomic, strong) UIScrollView *scrollView;
@end
...

- (void)viewDidLoad {
  [super viewDidLoad];

  // Create the scroll view.
  ...
  [self.view addSubview:scrollView];

  // Create a test button and link the ad request to its action.
  UIButton *loadBannerButton = [UIButton buttonWithType:UIButtonTypeCustom];
  ...
  [loadBannerButton addTarget:self
                       action:@selector(loadAd:)
             forControlEvents:UIControlEventTouchUpInside];
  [self.scrollView addSubview:loadBannerButton];

  // Construct the Ad Controller.
  GANSearchAdControllerOptions *options = [[GANSearchAdControllerOptions alloc] init];
  options.prefetchEnabled = YES;
  options.adType = GANSearchAdTypeSPA;
  options.adFetchCount = 3;

  _adController = [[GANSearchAdController alloc]
                     initWithPublisherID: @"your-client-id"
                                 styleID: @"your-settings-id"
                                 options: options
                                delegate: self];

  _adView = [_adController adView];
  [self.scrollView addSubview:_adView];
}

// Request ads when the test button is pressed.
- (void)loadAd:(id)sender {
  // Construct the Ad Request.
  GANSearchAdRequest *adRequest = [[GANSearchAdRequest alloc] init];
  adRequest.query =  @"some-query";
  // Start loading ads. Note that the loading is asynchronous.
  [_adController loadAds: adRequest];
}

// Insert ads into GANAdView if the request returns successfully.
- (void)searchAdController:(GANSearchAdController *)adController  
                didLoadAds:(NSInteger)numberOfAds {
  if (numberOfAds <= 0) {
    NSLog(@"No ads found on the server");
  } else {
    [_adController populateAdView:_adView identifier:@"demoAd"];
  }
}
...