Advanced Inline Ad Integration - adform/adform-ios-sdk GitHub Wiki

Setting custom ad size

It is possible to use custom size ad placements. To do so you just have to set ad size when creating AFAdInline object. Ad size can be set only before loading the ads, after the ads loading is started ad size cannot be changed. Easiest way to set ad size is to use convenience initializer initWithMasterTagId:adSize:.

Swift

override func viewDidLoad() {
    super.viewDidLoad()

    // Create a new ad inline object with Master tag id.
    let adView = AFAdInline(masterTagId: masterTag, presenting: self, adSize: CGSize(width: 320, height: 100))
    
    // Add the newly created ad view as a subview to your view controller's view.
    view.addSubview(adView)
    
    // Iniate ad loading.
    adView.loadAd()
}
Objective-C
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //Create a new ad inline object with Master tag id.
    AFAdInline *adView = [[AFAdInline alloc] initWithMasterTagId:mtag resentingViewController:self adSize:CGSizeMake(320, 100)];
    
    //Add the newly created ad view as a subview to your view controller's view.
    [self.view addSubview:adView];
    
    //Initiate ad loading.
    [adView loadAd];
}

Multiple ad size support

If you want to support multiple ad sizes in one placement you can use the additional dimensions feature. If you set additionalDimmensionsEnabled property to true, then this placement will display ads with any size retrieved from the server. This way the size of the ads can be controlled from the server. However, if you wish to limit supported sizes for the placement from the application side, you can do this by setting an array of sizes to supportedDimmensions property. This array must contain NSValue encoded CGSize structures. For convenience, you can use AFAdDimension or AFAdDimensionFromCGSize functions provided by the SDK to create NSValue objects.

Swift

// Create a new ad inline object with Master tag id.
let adView = AFAdInline(masterTagId: masterTag, presenting: self)

// Enable additional dimmensions.
adView.areAditionalDimmensionsEnabled = true

// Set supported dimensions.
adView.supportedDimmensions = [AFAdDimension(320, 50), AFAdDimension(320, 100), AFAdDimension(320, 150)]
Objective-C
// Create a new ad inline object with Master tag id.
AFAdInline *adView = [[AFAdInline alloc] initWithMasterTagId:mtag presentingViewController:self];

// Enable additional dimmensions.
adView.additionalDimmensionsEnabled = true;

// Set supported dimensions.
adView.supportedDimmensions = @[AFAdDimension(320, 50), AFAdDimension(320, 100), AFAdDimension(320, 150)];

Smart ad size

Smart ad size banners allow for the rendering of full-width ads across different devices and orientations by "smartly" sizing ad units according to screen size. Three ad heights (in dp, density-independent pixel) are available:

  • 32 - phones in landscape

  • 50 - phones in portrait

  • 90 - tablets in either orientation


More specifically, if the screen height of a device is between 400 and 720, an ad height of 50 is used in both orientations; for screen heights greater than 720, a 90 ad height is used.

When an image ad won't take up the entire allotted space for the banner, it will be centered.

To use banners with smart ad size, specify the constant AFSmartAdSize for the ad size:

Swift

// Create a new ad inline object with Master tag id.
let adView = AFAdInline(masterTagId: masterTag, presenting: self, adSize: AFSmartAdSize)

// Add the newly created ad view as a subview to your view controller's view.
view.addSubview(adView)

// Iniate ad loading.
adView.loadAd()
Objective-C
// Create a new smart size inline ad with Master tag id.
AFAdInline *adView = [[AFAdInline alloc] initWithMasterTagId:mtag resentingViewController:self adSize:AFSmartAdSize];

//Add the newly created ad view as a subview to your view controllers view.
[self.view addSubview:adView];

//Iniate ad loading.
[adView loadAd];

Controlling animations

AFAdInline state change to the expanded ad can be animated using a set of different animations. This can be achieved by setting the modalPresentationStyle property on AFAdInline object. At the moment SDK supports these animation types:

  • AFModalPresentationStyleSlide - When the modal view is presented, it slides up from the bottom of the screen (just like standard modal view controller presentation).
  • AFModalPresentationStyleFadeInOut - When the modal view is presented, it fades in and fades out when closed.
  • AFModalPresentationStyleNone - Presentation is not animated. You can use this presentation style if you want to disable animations.

By default, inline ads use the AFModalPresentationStyleSlide modal presentation animation type.

Controlling dim overlay in expanded state

It is possible to enable/disable application content dimming for AFAdInline expanded state by setting dimOverlayEnabled property.

Tracking ad state changes

You can use AFAdInlineDelegate protocol to get callbacks when ad view states change or events occur. By using this protocol you can track ad view load state and get callbacks about various events such as when ad view opens an internal web browser or a modal view. An example below shows you how to track ad view the loading state.

Swift

//We must define that our view controller conforms to AFAdInlineDelegate protocol
class ViewController: UIViewController, AFAdInlineDelegate {

...

override func viewDidLoad() {
    super.viewDidLoad()

    // Create a new ad inline object with Master tag id.
    let adView = AFAdInline(masterTagId: masterTag, presenting: self, adSize: AFSmartAdSize)
    
    //Set a delegate to the newly created AFAdView object.
    adView.delegate = self
    
    // Add the newly created ad view as a subview to your view controller's view.
    view.addSubview(adView)
    
    // Iniate ad loading.
    adView.loadAd()
}

...

// Finally we must define two methods which will get called when our ad view either finishes loading or fails to load.

func adInlineDidLoadAd(_ adInline: AFAdInline) {
    // This method gets called when an ad view finishes loading a new ad.
}

func adInlineDidFail(toLoadAd adInline: AFAdInline, withError error: Error) {
    // This method gets called when an ad view fails to load an ad.
    // An error object describes what went wrong.
}

Objective-C
...
//We must define that our view controller conforms to AFAdInlineDelegate protocol
@interface ViewController () <AFAdInlineDelegate>

...
	
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //Create an AFAdInline object.
    AFAdInline *adView = [[AFAdInline alloc] initWithMasterTagId:mtag presentingViewController:self];
    
    //Set a delegate to the newly created AFAdView object.
    adView.delegate = self;
    
    //Add it to view hierarchy and start loading.
    [self.view addSubview:adView];
    [adView loadAd];
} 


...
	
//Finally we must define two methods which will get called when our ad view either finishes loading or fails to load.
	
- (void)adInlineDidLoadAd:(AFAdInline *)adInline {
    //This method gets called when an ad view finishes loading a new ad.
}

- (void)adInlineDidFailToLoadAd:(AFAdInline *)adInline withError:(NSError *)error {
    //This method gets called when an ad view fails to load an ad.
    //An error object describes what went wrong.
}

Responding to AdInline size changes

AFAdInline placement is shown only when an advertisement is loaded. If no ad is loaded it stays hidden. Transitions between these states are automatically animated, so you should reposition your applications content accordingly. To do so you must implement AFAdInlineDelegate protocol methods: adInlineWillShow: and adInlineWillHide. The example code provided below shows you how to reposition UITextView to prevent its content from being hidden by the ad view. You should not rely on the AFAdInline frame property to determine its size and instead use adSize property.

Swift

func adInlineWillShow(_ adInline: AFAdInline) {
    let insets = textView.contentInset
    insets.bottom += adView.adSize.height
    let scrollInsets = textView.contentInset
    scrollInsets.bottom += adView.adSize.height
    
    UIViewPropertyAnimator.runningPropertyAnimator(
        withDuration: AFAdViewAnimationDuration,
        delay: 0,
        options: [],
        animations: {
            self.textView.contentInset = insets
            self.textView.scrollIndicatorInsets = scrollInsets
        },
        completion: nil
    )
}

func adInlineWillHide(_ adInline: AFAdInline) {
    let insets = textView.contentInset
    insets.bottom -= adView.adSize.height
    let scrollInsets = textView.contentInset
    scrollInsets.bottom -= adView.adSize.height
    
    UIViewPropertyAnimator.runningPropertyAnimator(
        withDuration: AFAdViewAnimationDuration,
        delay: 0,
        options: [],
        animations: {
            self.textView.contentInset = insets
            self.textView.scrollIndicatorInsets = scrollInsets
        },
        completion: nil
    )
}
Objective-C
- (void)adInlineWillShow:(AFAdView *)adView {
    UIEdgeInsets insets = _textView.contentInset;
    insets.bottom += adView.adSize.height;
    UIEdgeInsets scrollInsets = _textView.contentInset;
    scrollInsets.bottom += adView.adSize.height;
    [UIView animateWithDuration:AFAdViewAnimationDuration
                     animations:^{
        _textView.contentInset = insets;
        _textView.scrollIndicatorInsets = scrollInsets;
    }];
}

- (void)adInlineWillHide:(AFAdView *)adView {
    UIEdgeInsets insets = _textView.contentInset;
    insets.bottom -= adView.adSize.height;
    UIEdgeInsets scrollInsets = _textView.contentInset;
    scrollInsets.bottom -= adView.adSize.height;
    [UIView animateWithDuration:AFAdViewAnimationDuration
                     animations:^{
        _textView.contentInset = insets;
        _textView.scrollIndicatorInsets = scrollInsets;
    }];
}

Fallback

If our ad view fails to load, you can use adInlineDidFailToLoadAd:withError: method to add any backup logic, such as loading an ad view from another SDK or display a local image banner but don't forget that AFAdInline auto-refreshes.

The most common case to replace SDK with the fallback image in case of failure:

Swift

func adInlineDidFail(toLoadAd adInline: AFAdInline, withError error: Error) {
    let imageView = UIImageView(image: .init(named: "banner_fallback.png"))
    imageView.frame = adInline.frame
    imageView.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleTopMargin]
    view.addSubview(imageView)
}
Objective-C
- (void)adInlineDidFailToLoadAd:(AFAdInline *)adInline withError:(NSError *)error {        
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"banner_fallback.png"]];
        
    imageView.frame = adInline.frame;
    imageView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin);
        
    [self.view addSubview:imageView];
}

And the result will be image fallback:

alt tag

⚠️ **GitHub.com Fallback** ⚠️