Skip to content

Advanced Interstitial Ad Integration

Vladas Drejeris edited this page Jun 4, 2021 · 2 revisions

Controlling ads display with AFPageViewControllerMediator

AFPageViewControllerMediator can display ads at a certain frequency or at specific pages. To show ads with frequency, you just have to use the convenience initializer initWithMasterTagId:adFrequency:pageViewController: and set desired ad frequency. The mediator will ensure that ads are displayed as often as specified by the frequency.

Swift

// To display ads every 5-th page, pass 5 as ad frequency.
self.mediator = AFPageViewControllerMediator(masterTagId: masterTag, adFrequency: 5, pageViewController: self.pageViewController)
Objective-C
// To display ads every 5-th page, pass 5 as ad frequency.
self.mediator = [[AFPageViewControllerMediator alloc] initWithMasterTagId:MsterTagId
    	                                                      adFrequency:5
         	                                       pageViewController:self.pageViewController];

To display ads on certain pages you have to implement AFPageViewControllerMediatorDelegate protocol. This protocol has two methods which ask if ad should be displayed after (pageViewControllerMediator:shouldShowAdAfterViewController:) and before (pageViewControllerMediator:shouldShowAdBeforeViewController:) specific view controller. You can implement these methods to decide when to display ads. The first method decides if ads should be displayed when the user is scrolling forward and the other one when the user is scrolling back.

Swift

func pageViewControllerMediator(_ mediator: AFPageViewControllerMediator!, shouldShowAdAfter viewController: UIViewController!) -> Bool {
    // Here you can decide if the ad should be displayed after the view controller.
    return true
}

func pageViewControllerMediator(_ mediator: AFPageViewControllerMediator!, shouldShowAdBefore viewController: UIViewController!) -> Bool {
    // Here you can decide if the ad should be displayed before the view controller.
    return true
}
Objective-C
- (BOOL)pageViewControllerMediator:(AFPageViewControllerMediator *)mediator shouldShowAdAfterViewController:(UIViewController *)viewController {
    // Here you can decide if the ad should be displayed after the view controller.
    return TRUE;
}

- (BOOL)pageViewControllerMediator:(AFPageViewControllerMediator *)mediator shouldShowAdBeforeViewController:(UIViewController *)viewController {
    // Here you can decide if the ad should be displayed before the view controller.
    return TRUE;
}

Controlling ads display with AFCollectionViewMediator

The mediator can display ads at a certain frequency or at specific pages. To show ads with frequency, you just have to use convenience initializer initWithMasterTagId:adFrequency:collectionView:presentingViewController: and set desired ad frequency. The mediator will ensure that ads are displayed as often as specified by frequency.

Swift

// To display ads every 5-th page, pass 5 as ad frequency.
self.mediator = AFCollectionViewMediator(
    masterTagId: masterTag,
    adFrequency: 5,
    collectionView: collectionView,
    presenting: self
)
Objective-C
// To display ads every 5-th page, pass 5 as ad frequency.
self.mediator = [[AFPageViewControllerMediator alloc] initWithMasterTagId:MsterTagId
                                                              adFrequency:5
      	                                                   collectionView:self.collectionView
       	                                         presentingViewController:self;

To display ads on certain pages you have to implement AFCollectionViewMediatorDelegate protocol. This protocol has one method which asks if the ad should be displayed at the index path (collectionViewMediator:shouldShowAdAtIndexPath:). You can implement this method to decide when to display ads.

Swift

func collectionViewMediator(_ mediator: AFCollectionViewMediator!, shouldShowAdAt indexPath: IndexPath!) -> Bool {
    // Here you can decide if the ad should be displayed at the index path.
    return true
}
Objective-C
- (BOOL )collectionViewMediator:(AFCollectionViewMediator *)mediator shouldShowAdAtIndexPath:(NSIndexPath *)indexPath {
    // Here you can decide if the ad should be displayed at the index path.
    return YES;
}

Accessing UICollectionView cells directly while using AFCollectionViewMediator

AFCollectionViewMediator uses different internal index paths to communicate with the collection view. Therefore, if you want to access collection view cells or index paths directly from collection view, e.g. use methods indexPathsForSelectedItems: indexPathsForVisibleItems, selectItemAtIndexPath: or cellForItemAtIndexPath: you should first convert the index paths you have to the inner index paths (used by the mediator to communicate to collection view) or outer index paths (outer index path, used by the mediator to communicate with data source provided by you). Only then use the index paths to interact directly with the collection view. For index path conversion use AFCollectionViewMediator methods: indexPathFromOuterIndexPath:, outerIndexPathFromIndexPath.

Clone this wiki locally