Wiki

Clone wiki

mngads-demo-ios / dfp-adapter-ios

iOS DFP Adapter

This guide shows you how to integrate [MNGAds] mediation adapter of Google Mobile Ads SDK with your current iOS app and set up additional request parameters.

Supported ad formats

  • Banners
  • Interstitials
  • Native Ads

Requirements

  • Use Xcode 11 or higher
  • Target iOS 10.0 or higher

Google Ad Manager UI

The custom event must be defined in the Google Ad Manager UI.

1. Create a Yield groups

  • Create an Ad Network screenshot-admanager.google.com-2019.10.02-22_36_16 (1).jpg
  1. Create a Company with Madvertise Ad-Network name
  2. Choose Appsfire network
  3. Allow mediation

2. Add a Yield Group

screenshot-admanager.google.com-2019.10.08-18_32_29.jpg

  1. Choose a name
  2. Choose a format (You must create a Yield Group by format)
  3. Choose at least one size according format
  4. Target a specific placement

3. Add A Yield Partner and Define a custom event

On your Google Ad Manager UI, create a custom event

screenshot-admanager.google.com-2019.10.08-18_33_45.jpg

  1. Select Madvertise Ad-Network created on Step 1
  2. Choose Custom Event for Integration type
  3. Choose your platform iOS or Android
  4. Set the following Label, class Name and Parameter according your format :
  • Banner : Label= MadvertiseCustomEventBanner, class Name= MadvertiseCustomEventBanner and Parameter= /YOUR_APP_ID/PLACEMENT_ID_BANNER
  • Interstitial : Label= MadvertiseCustomEventInterstitial, class Name= MadvertiseCustomEventInterstitial and Parameter= /YOUR_APP_ID/PLACEMENT_ID_INTER
  • Native Ad : Label= MadvertiseCustomEventNativead, class Name= MadvertiseCustomEventNativead and Parameter= /YOUR_APP_ID/PLACEMENT_ID_NATIVEAD

Integrate MNGAds in your application project

1. Set Up

#import "MadvertiseCustomEventInterstitial.h"
#import "MadvertiseCustomEventBanner.h"
#import "MadvertiseCustomEventNativead.h"
@import GoogleMobileAds;

2. Initialize your ads

You can check our Demo page.

2.1 Banner and Interstitial:

You may now use MNG DFP Adaptor to show Interstitial Ads and Banner Ads the same way it's described in the DFP Documentation.The adapter code and the setup you did on your Google Ad Manager UI will allow MNG Ads to deliver ads.

You must pass CUSTOMLABELADAPTER with custom event adapter class name for and Passed the ViewController to the Adapter:

  • Banner : MadvertiseCustomEventBanner :
    MadvertiseCustomEventBanner * customEventBanner = [[MadvertiseCustomEventBanner alloc] init];
    [customEventBanner setViewController:self];    
Handle callBack from GADBannerViewDelegate

Through the use of GADBannerViewDelegate, you can listen for lifecycle events, such as when an ad is closed or the user leaves the app.

  • Registering for banner events :
    _bannerView.delegate = self;
  • Tells the delegate an ad request loaded an ad:
- (void)adViewDidReceiveAd:(nonnull GADBannerView *)bannerView{

}
  • Tells the delegate an ad request failed..
- (void)adView:(nonnull GADBannerView *)bannerView
didFailToReceiveAdWithError:(nonnull GADRequestError *)error{

}

Interstitial

  • Interstitial : MadvertiseCustomEventInterstitial :
 MadvertiseCustomEventInterstitial * customEventInterstitial = [[MadvertiseCustomEventInterstitial alloc]init];
    [customEventInterstitial setViewController: YourViewController];
Handle callBack from GADInterstitialDelegate
  • set the GADInterstitialDelegate and the viewController and add "GADInterstitialDelegate" in the interface viewController :
_interstitial.delegate = self;
  • will be called by the SDK when your Interstitial is ready. Interstitial will be showen:
- (void)interstitialWillPresentScreen:(GADInterstitial *)interstitial {

}
  • interstitialWillDismissScreen: Called before the interstitial is to be animated off the screen.
-(void)interstitialWillDismissScreen:(GADInterstitial *)ad{

}

Native Ads

Init MadvertiseCustomEventNativead

To create a nativeAd you have to init an object with type MadvertiseCustomEventNativead and set the DFPNativeAdLoadedDelegate to the self view controller.

   [MadvertiseCustomEventNativead setMainImage:_mainImageView andIconeImage:_iconImageView andNativeView:_nativeView andButton:_callToActionLabel andWithViewController:self andTitleLabel:_titleLabel andMainLabel:_mainTextLabel];

    [MadvertiseCustomEventNativead setNativeAdMadvertiseDFPDelegate:self];

    GADNativeAdViewAdOptions *adViewOptions = [[GADNativeAdViewAdOptions alloc] init];
    adLoader = [[GADAdLoader alloc] initWithAdUnitID: NATIVE_AD_ADUNIT rootViewController:self adTypes:@[kGADAdLoaderAdTypeUnifiedNative] options:@[adViewOptions]];
   [MadvertiseCustomEventNativead setNativeAdMadvertiseDFPDelegate:self];

    DFPRequest* request = [DFPRequest request];
    CLLocation *currentLocation = _locationManager.location;
    if (currentLocation) {
        [request setLocationWithLatitude:currentLocation.coordinate.latitude
                               longitude:currentLocation.coordinate.longitude
                                accuracy:currentLocation.horizontalAccuracy];
    }
        request.keywords = [self setRequestKeyword:@"target=mngadsdemo;version=2.15.1"];

 request.customTargeting = @{@"key1" : @"key1vaule", @"key2" :@"key2vaule"};
    GADCustomEventExtras *extras = [[GADCustomEventExtras alloc] init];
    [extras setExtras:request.customTargeting forLabel:@"MadvertiseCustomEventNativead"];
    [request registerAdNetworkExtras:extras];
    [adLoader loadRequest:request];
Handle callBack from NativeAdMadvertiseDFPDelegate

madvertiseCustomEventNativeAdidReceiveAd: will be called when the nativeObject is ready. now you can create your own view.

-(void)madvertiseCustomEventNativeAdidReceiveAd:(MNGNAtiveObject *)nativeDFPObject{
    _nativeView.hidden = NO;
    [self.view bringSubviewToFront:_nativeView];
    [_callToActionLabel setTitle:nativeDFPObject.callToAction forState:UIControlStateNormal];
    _titleLabel.text = nativeDFPObject.title;
    _mainTextLabel.text = nativeDFPObject.body;
    [nativeDFPObject registerViewForInteraction:_nativeView withMediaView:_mainImageView withIconImageView:_iconImageView withViewController:self withClickableView:_callToActionLabel];
}
}

adLoader:didFailToReceiveAdWithError: will be called when all ad request fail. it will return the error of last called ad request.

-(void)madvertiseCustomEventNativeAd:(NSObject *)adapter didFailToLoadWithError:(NSError *)error{
    NSLog(@"native dfp Object did fail");
}

3. Location

  • If a user has granted your app location permissions, Ad Manager automatically passes this location data to the SDK. The SDK uses this data to improve ad targeting without requiring any code changes in your app.
  • You can specify location-targeting information in the ad request as follows:
 DFPRequest* request = [DFPRequest request];
    CLLocation *currentLocation = _locationManager.location;
    if (currentLocation) {
        [request setLocationWithLatitude:currentLocation.coordinate.latitude
                               longitude:currentLocation.coordinate.longitude
                                accuracy:currentLocation.horizontalAccuracy];
    }

4. Custom targeting

If you need to send your preferences (Age, Keyword, Content URL) use the GADCustomEventExtras setExtras method.

    DFPRequest* request = [DFPRequest request];
    request.customTargeting = @{@"key1" : @"key1vaule", @"key2" :@"key2vaule"};
    GADCustomEventExtras *extras = [[GADCustomEventExtras alloc] init];
    [extras setExtras:request.customTargeting forLabel:@"MadvertiseCustomEventBanner"];
    [request registerAdNetworkExtras:extras];

Updated