inmobi-ad-tracker-iat | Audience Bidding for MoPub (iOS)

Setup

In order to start setting up, you’ve to ensure that the steps mentioned in the InMobi Integration have already been incorporated.

Step 1: Initialising the InMobi SDK

Option 1. Initialise InMobi SDK while Initialisating MoPub SDK using MPMoPubConfiguration. Follow instructions as mentioned here

#import <InMobiMoPubPlugin/InMobiAdapterConfiguration.h> 
MPMoPubConfiguration *sdkConfig = [[MPMoPubConfiguration alloc] 
    initWithAdUnitIdForAppInitialization: <MoPubAppId>]; 
sdkConfig.additionalNetworks = [NSArray 
    arrayWithObject:InMobiAdapterConfiguration.class]; 
NSMutableDictionary *inmobiDict = [[NSMutableDictionary alloc] 
    initWithObjectsAndKeys:@"<InMobiAccountID>", @"accountid", nil]; 
NSMutableDictionary *configDictionary = [[NSMutableDictionary alloc] 
    initWithObjectsAndKeys:inmobiDict, @"InMobiAdapterConfiguration", nil]; 
sdkConfig.mediatedNetworkConfigurations = configDictionary; 
[[MoPub sharedInstance] initializeSdkWithConfiguration:sdkConfig 
    completion:^{ 
        NSLog(@"Mopub and third party SDKs are initialised"); 
}];
	

Note : Import the InMobiAdapterConfiguration.h header

Option 2: Initialise InMobi SDK directly

NSError* error= nil; 
[InMobiSDKInitialiser initialiseSDK:[info valueForKey:kIMAccountID] 
    withError:&error];
	

Note : Initialize once per app’s lifecycle, typically on app launch, using valid account ID for your app. Make no ad requests until the SDK initialization has completed.

Step 2: Adding the InMobiMoPubPlugin to your Project

Option 1: Using CocoPods

pod 'InMobiMoPubPlugin'
  pod 'InMobiSDK'
	

Note : Add pod 'mopub-ios-sdk' for resolving dependancy.

Option 2: Adding the SDK Library to your Application Project

Add the framework in your project. Download the MoPub-InMobi plugin from here

Note : If the version is X.Y.Z.A , X.Y.Z denotes the compatible media SDK version whereas A denotes the patch number.

Banner Ads

Step 1: Setting up and adding Banner Ad to your App

Creating a Banner Ad

To add a banner ad in iOS code, create an instance of IMABMoPubBanner:

 IMABMoPubBanner* bannerAd = [[IMABMoPubBanner alloc] initWithSize:<size> 
    placementId:<placementID> delegate:<delegate>];
	

Notes

  • The IMABMoPubBanner class is not thread-safe. A banner instance must be created on the main thread.
  • Similarly, all methods on this instance must be called on the main thread. Not doing so will lead to unpredictable behavior and may affect your ability to monetize with InMobi.

Step 2: Requesting for Bid

Requesting for a bid requires you to call the IMABMoPubBanner: requestBid method on the instance you just created to request for a bid. Note that there are two API(s) available to request for a bid -

  • IMABMoPubBanner: -(void)requestBid:(nullable id)ad timeout:(NSTimeInterval)timeout; When you pass a MoPubView instance, the InMobi SDK automatically takes care of setting the keyword to this instance and saving the IMBanner instance for future usage in CustomEvent class.
    /** 
    * A sample code to request bid with MoPubView 
    */ 
    @interface BannerViewController () <IMABMoPubDelegate, MPAdViewDelegate> 
    //Hold the instance of IMABMoPubBanner, to load an InMobi's ad. 
    @property (nonatomic, strong) IMABMoPubBanner* abMoPubBanner; 
    @property (nonatomic, strong) MPAdView* mpBanner; 
    @end 
    @implementation BannerViewController 
    - (void)loadUABannerAd { 
        // Write code to initialise your MoPub ad object and then do the following 
        self.mpBanner = [[MPAdView alloc] initWithAdUnitId:<adUnitID> size:
            <adSize>]; 
        self.mpBanner.delegate = <delegate>; 
        //Create an instance of IMABMoPubBanner and call requestBid 
        self.abMoPubBanner = [[IMABMoPubBanner alloc] initWithSize:<size> 
            placementId:<placementID> delegate:<delegate>]; 
        [self.abMoPubBanner requestBid:self.mpBannerTop timeout:<timeout>]; 
    } 
    #pragma mark - IMABMopubBannerDelegate method 
    /** 
    * Notifies Bid is recieved successfully with transaction keyword. 
    * @param mpAd MoPub ad object passed in requestBid. It can be either MPAdView or MPInterstitialAdController type 
    *          It will be null if the MoPub ad object was not passed while calling the requestBid API 
    * @param imAd InMobi ad Object for which the keyword is recieved. In case of nil mpAd, set imAd inside localExtras of mopub 
    *          ad object. Kindly refer intergation document. 
    * @param keyword  keyword derived from the bid received. 
    */ 
    - (void)bidReceivedFor:(nullable id)mpAd andInMobiAd:(id)imAd 
        withTransactionInfo:(NSString*)keyword { 
            NSLog(@"Test MP Banner:- bidReceivedFor withTransactionInfo: %@", keyword); 
            [self.mpBanner loadAd]; 
            //add mpBanner to the subView. 
    } 
    /** 
    * Notifies Bid fetch is failed with error. 
    * @param ad MoPub ad object passed in requestBid. It can be either MPAdView or MPInterstitialAdController type 
    *          It will be null if the MoPub ad object was not passed while calling the requestBid API 
    * @param error error which caused failure. 
    */ 
    - (void)bidFailedFor:(nullable id)ad withError:(NSError*)error { 
        NSLog(@"Test MP Banner:- bidFailedForAd withError: %@", error); 
    } 
    @end
    		
  • IMABMoPubBanner: -(void)requestBid:(NSTimeInterval)timeout; In case you don’t wish to pass the MoPubView instance, then you need to take care of not only setting the keyword to the MoPubView instance (for which bid was requested) but also you’ve to pass IMBanner instance to CustomEvent for rendering the ad, for that we create an instance of IMFacadeWrapper and add it in the localExtras of MPAdView, as mentioned in the following code:
    /** 
    * A sample code to request bid without MoPubView 
    */ 
    @interface BannerViewController () <IMABMoPubDelegate, MPAdViewDelegate> 
    //Hold the instance of IMABMoPubBanner, to load an InMobi's ad. 
    @property (nonatomic, strong) IMABMoPubBanner* abMoPubBanner; 
    @property (nonatomic, strong) MPAdView* mpBanner; 
    @end 
    @implementation BannerViewController 
    - (void)loadUABannerAd { 
        // Write code to initialise your MoPub ad object and then do the following 
        self.mpBanner = [[MPAdView alloc] initWithAdUnitId:<adUnitID> size:
            <adSize>]; 
        self.mpBanner.delegate = <delegate>; 
        //Create an instance of IMABMoPubBanner and call requestBid 
        self.abMoPubBanner = [[IMABMoPubBanner alloc] initWithSize:<size> 
            placementId:<placementID> delegate:<delegate>]; 
        [self.abMoPubBanner requestBid:<timeout>]; 
    } 
    #pragma mark - IMABMopubBannerDelegate method 
    /** 
    * Notifies Bid is recieved successfully with transaction keyword. 
    * @param mpAd MoPub ad object passed in requestBid. It can be either MPAdView or MPInterstitialAdController type 
    *          It will be null if the MoPub ad object was not passed while calling the requestBid API 
    * @param imAd InMobi ad Object for which the keyword is recieved. In case of nil mpAd, set imAd inside localExtras of mopub 
    *          ad object. Kindly refer intergation document. 
    * @param keyword  keyword derived from the bid received. 
    */ 
    - (void)bidReceivedFor:(nullable id)mpAd andInMobiAd:(id)imAd 
        withTransactionInfo:(NSString*)keyword { 
            //mpAd is null. 
            NSLog(@"Test MP Banner:- bidReceivedFor withTransactionInfo: %@", keyword); 
            IMFacadeWrapper* facadeWrapper = [[IMFacadeWrapper alloc] 
                initWithMoPubObject:self.mpAdView andInMobiObject:imAd]; 
            [self.mpBanner setKeywords:keyword]; // set keyword 
            self.mpBanner.localExtras = @{kIMABInMobiObjectKey : facadeWrapper}; 
            [self.mpBanner loadAd]; 
            //add mpBanner to the subView. 
    } 
    /** 
    * Notifies Bid fetch is failed with error. 
    * @param ad MoPub ad object passed in requestBid. It can be either MPAdView or MPInterstitialAdController type 
    *          It will be null if the MoPub ad object was not passed while calling the requestBid API 
    * @param error error which caused failure. 
    */ 
    - (void)bidFailedFor:(nullable id)ad withError:(NSError*)error { 
        NSLog(@"Test MP Banner:- bidFailedForAd withError: %@", error); 
    } 
    @end
    		

Notes :

The keyword received will be in the format of "bidKeyword": "IMAB:9.00, IMAB:9.10"/ “IMAB:9.00” It’s a passthrough key, kindly add bidKeyword as keyword of MoPub object. The first bid value is the absolute keyword and the second bid keyword is granular keyword.

Note that the IMAB keyword set to the MoPubView must be cleared off post each ad opportunity and before loading the banner ad again.

Passing mopub object in facadeWrapper is optional. If mopub object will be passed to IMFacadeWrapper, the keyword removal will be handled.

Step 3: Setting up for MoPubView refresh

In case your MoPubView has auto refresh feature enabled, then simply you need to request a bid for every success/failure for callback.

- (void)adViewDidLoadAd:(MPAdView *)view adSize:(CGSize)adSize { 
	NSLog(@"-------- MPAdView, adViewDidLoadAd:"); 
	 [self.abBanner requestBid:self.mpAdView timeout:]; 
} 

- (void)adView:(MPAdView *)view didFailToLoadAdWithError:(NSError *)error { 
	NSLog(@"-------- MPAdView, didFailToLoadAdWithError:"); 
	 [self.abBanner requestBid:self.mpAdView timeout:]; 
} 
	

Interstitial Ads

Step 1: Setting up and adding an Interstitial Ad to your App

Creating an Interstitial Ad

To create an interstitial ad, create an instance of an IMABMoPubInterstitial like so:

IMABMoPubInterstitial* interstitialAd = [[IMABMoPubInterstitial alloc] initWithPlacementId:<placementID> 
	delegate:<delegate>];
	

Notes:

  • The IMABMoPubInterstitial class is not thread-safe. An interstitial instance must be created on the UI thread.
  • Similarly, all methods on this instance must be called on the UI thread. Not doing so will lead to unpredictable behavior and may affect your ability to monetize with InMobi.

Step 2: Requesting for Bid

Requesting for a bid requires you to call the IMABMoPubInterstitial: requestBid method on the instance you just created to request for a bid. Note that there are two API(s) available to request for a bid -

  • IMABMoPubInterstitial: -(void)requestBid:(nullable id)ad timeout: (NSTimeInterval) timeout; When you pass a MPInterstitialAdController instance, the InMobi SDK automatically takes care of setting the keyword to this instance and saving the IMInterstitial instance for future usage in CustomEvent class.
    /** 
    * A sample code to request bid with MPInterstitialAdController 
    */ 
    @interface InterstitialViewController () <IMABMoPubDelegate, MPInterstitialAdControllerDelegate> 
    //Hold the instance of IMABMoPubInterstitial, to load an InMobi's ad. 
    @property (strong, nonatomic) MPInterstitialAdController *mpInterstitial; 
    @property (strong, nonatomic) IMABMoPubInterstitial *abInterstitial; 
    @end 
    @implementation InterstitialViewController 
    - (void)loadUABannerAd { 
        // Write code to initialise your MoPub ad object and then do the following 
        self.mpInterstitial = [MPInterstitialAdController interstitialAdControllerForAdUnitId:<adUnitID>]; 
        self.mpInterstitial.delegate = <delegate>; 
        //Create an instance of IMABMoPubInterstitial and call requestBid 
        self.abInterstitial = [[IMABMoPubInterstitial alloc]initWithPlacementId:<placementID> delegate:<delegate>]; 
        [self.abInterstitial requestBid:self.mpInterstitial timeout:<timeout>];
    } 
    #pragma mark - IMABMopubBannerDelegate method
    /** 
    * Notifies Bid is recieved successfully with transaction keyword. 
    * @param mpAd MoPub ad object passed in requestBid. It can be either MPAdView or MPInterstitialAdController type 
    * It will be null if the MoPub ad object was not passed while calling the requestBid API 
    * @param imAd InMobi ad Object for which the keyword is recieved. 
    * In case of nil mpAd, set imAd inside localExtras of mopub 
    * ad object. Kindly refer intergation document. 
    * @param keyword  keyword derived from the bid received. 
    */ 
    - (void)bidRecievedFor:(nullable id)mpAd andInMobiAd:(id)imAd withTransactionInfo:(NSString*)keyword { 
    	//mpAd is null. 
        NSLog(@"Test MP Banner:- bidRecievedFor withTransactionInfo: %@", keyword); 
        [self.mpInterstitial loadAd]; 
    } 
    /** 
    * Notifies Bid fetch is failed with error. 
    * @param ad MoPub ad object passed in requestBid. It can be either MPAdView or MPInterstitialAdController type 
    * It will be null if the MoPub ad object was not passed while calling the requestBid API 
    * @param error error which caused failure. 
    */ 
    - (void)bidFailedFor:(nullable id)ad withError:(NSError*)error {
        NSLog(@"Test MP Banner:- bidFailedForAd withError: %@", error); 
    } 
    @end
    		
  • IMABMoPubInterstitial: -(void)requestBid:(NSTimeInterval)timeout; In case you don’t wish to pass the MPInterstitialAdController instance, then you need to take care of not only setting the keyword to the MPInterstitialAdController instance (for which bid was requested) but also you’ve to pass IMInterstitial instance to CustomEvent for rendering the ad, for that we create an instance of IMFacadeWrapper and add it in the localExtras of MPInterstitialAdController, as mentioned in the following code:
    /** 
    * A sample code to request bid without MPInterstitialAdController 
    */ 
    @interface InterstitialViewController () <IMABMoPubDelegate, 
        MPInterstitialAdControllerDelegate> 
    //Hold the instance of IMABMoPubInterstitial, to load an InMobi's ad. 
    @property (strong, nonatomic) MPInterstitialAdController *mpInterstitial; 
    @property (strong, nonatomic) IMABMoPubInterstitial *abInterstitial; 
    @end 
    @implementation InterstitialViewController 
    - (void)loadUAInterstitialAd { 
        // Write code to initialise your MoPub ad object and then do the following 
        self.mpInterstitial = [MPInterstitialAdController 
            interstitialAdControllerForAdUnitId:<adUnitID>]; 
        self.mpInterstitial.delegate = <delegate>; 
        //Create an instance of IMABMoPubInterstitial and call requestBid 
        self.abInterstitial = [[IMABMoPubInterstitial alloc]
            initWithPlacementId:<placementID> delegate:<delegate>]; 
        [self.abInterstitial requestBid:<timeout>]; 
    } 
    #pragma mark - IMABMopubBannerDelegate method 
    /** 
    * Notifies Bid is recieved successfully with transaction keyword. 
    * @param mpAd MoPub ad object passed in requestBid. It can be either MPAdView or MPInterstitialAdController type 
    *          It will be null if the MoPub ad object was not passed while calling the requestBid API 
    * @param imAd InMobi ad Object for which the keyword is recieved. In case of nil mpAd, set imAd inside localExtras of mopub 
    *          ad object. Kindly refer intergation document. 
    * @param keyword  keyword derived from the bid received. 
    */ 
    - (void)bidReceivedFor:(nullable id)mpAd andInMobiAd:(id)imAd 
        withTransactionInfo:(NSString*)keyword { 
            //mpAd is null. 
            NSLog(@"Test MP Banner:- bidReceivedFor withTransactionInfo: %@", keyword); 
            IMFacadeWrapper* facadeWrapper = [[IMFacadeWrapper alloc] 
                initWithMoPubObject:self.mpInterstitial andInMobiObject:imAd]; 
            [self.mpInterstitial setKeywords:keyword]; // set keyword 
            self.mpInterstitial.localExtras = @{kIMABInMobiObjectKey : 
                facadeWrapper}; 
            [self.mpInterstitial loadAd]; 
            //add mpBanner to the subView. 
    } 
    /** 
    * Notifies Bid fetch is failed with error. 
    * @param ad MoPub ad object passed in requestBid. It can be either MPAdView or MPInterstitialAdController type 
    *          It will be null if the MoPub ad object was not passed while calling the requestBid API 
    * @param error error which caused failure. 
    */ 
    - (void)bidFailedFor:(nullable id)ad withError:(NSError*)error { 
        NSLog(@"Test MP Banner:- bidFailedForAd withError: %@", error); 
    } 
    @end
    		

Notes :

The keyword received will be in the format of "bidKeyword": "IMAB:9.00, IMAB:9.10"/ “IMAB:9.00” It’s a passthrough key, kindly add bidKeyword as keyword of MoPub object. The first bid value is the absolute keyword and the second bid keyword is granular keyword.

Note that the IMAB keyword set to the MPInterstitialAdController must be cleared off post each ad opportunity and before loading the banner ad again.

Passing mopub object in facadeWrapper is optional. If mopub object will be passed to IMFacadeWrapper, the keyword removal will be handled.

After load success/failure callback, new bid request can be called.