iOS Sample - ONEMobile/SDK GitHub Wiki
###Sample Code Snippet###
Initializing
Import the LinDFPBanner.h and GoogleMobileAds/GoogleMobileAds.h like below
#import "LinDFPBanner.h"
#import "<GoogleMobileAds/GoogleMobileAds.h>"
Code Snippet For Banner Ad
Create a UIView from storyboard or xib to your viewcontroller class and make it as a subclass of LinDFPBanner and hook it as the following sample code.
@property (strong, nonatomic) IBOutlet LinDFPBanner *linBannerView;
Implement the GADAdSizeDelegate method to detect an ad size change and GADBannerViewDelegate to detect ad's lifecycle
In viewDidload write the below code:-
// For single ad
self.linBannerView.adSize=GADAdSizeFromCGSize(CGSizeMake(xx,yy));
// For multiple sizes ad
NSArray *adSizesArray=@[
NSValueFromGADAdSize(GADAdSizeFromCGSize(CGSizeMake(w1, h1))),
NSValueFromGADAdSize(GADAdSizeFromCGSize(CGSizeMake(w2, h2))),
NSValueFromGADAdSize(GADAdSizeFromCGSize(CGSizeMake(w3, h3)))
];
self.linBannerView.validAdSizes = adSizesArray;
self.linBannerView.delegate=self;
Remember to set the delegate before making the request for an ad.
self.linBannerView.adSizeDelegate = self;
self.linBannerView.adUnitID = @”/9331149/66082029/campaign_test”;
self.linBannerView.pubID=@”lin-xxxxxxxxxx”;
self.linBannerView.rootViewController = self;
For pubID contact [email protected]
NSDictionary *preferences=@{
@"GeoInfo":@"YES",
@"NetworkInfo":@"YES"
};
Child-directed setting
For purposes of the Children's Online Privacy Protection Act (COPPA), there is a setting called "tag for child directed treatment". As an app developer, you can indicate whether you want Google to treat your content as child-directed when you make an ad request. If you indicate that you want Google to treat your content as child-directed, we will take steps to disable IBA and remarketing ads on that ad request.
- If you set tagForChildDirectedTreatment to YES, you will indicate that your content should be treated as child-directed for purposes of COPPA.
- If you set tagForChildDirectedTreatment to NO, you will indicate that your content should not be treated as child-directed for purposes of COPPA.
- If you do not set tagForChildDirectedTreatment, ad requests will include no indication of how you would like your content treated with respect to COPPA. [[LinDFPBanner LinRequest] tagForChildDirectedTreatment:YES];
Content URL
Applications that monetize content matching a webpage's content may pass a content URL for keyword targeting.
For example, if your application serves blog articles and is requesting an ad while showing content from the article http://googleadsdeveloper.blogspot.com/2013/10/upgrade-to-new-google-mobile-ads-sdk.html, then you can pass this URL to target relevant keywords:
[LinDFPBanner LinRequest].contentURL = @"http://xxx-yyy.com";
App events
App events allow you to create ads that can send messages to their application code. The application can then take actions based on these messages.You can listen for DFP specific app events using GADAppEventDelegate. These events may occur at any time during the ad's lifecycle, even before GADBannerViewDelegate's adViewDidReceiveAd: is called.
Remember to set the delegate using the appEventDelegate property before making the request for an ad.
Declare GADAppEventDelegate
self.linBannerView.appEventDelegate=self;
// App event delegate
- (void)adView:(DFPBannerView *)banner didReceiveAppEvent:(NSString *)name
withInfo:(NSString *)info {
}
Manual impression counting
Manual impression counting is compatible only with direct-sold and house campaigns (i.e., creatives trafficked directly in DFP). It should not be used for backfill or third-party networks ads. For more details, see this help center article.You can manually send impression pings to DFP if you have special conditions for when an impression should be recorded. This can be done by first enabling a linBannerView for manual impressions prior to loading an ad:
self.linBannerView.enableManualImpressions = YES;
When you determine that an ad has been successfully returned and is on screen, you can manually fire an impression:
[self.linBannerView recordImpression];
Note: Impressions are automatically fired when the adViewDidReceiveAd callback is invoked. You should only consider using this feature if you want to record impressions at a different point in time.
[self.linBannerView loadRequestLin:[LinDFPBanner LinRequest]
banner:self.linBannerView preferences:preferences];
Delegate For Banner Ad
- (void)adViewDidReceiveAd:(DFPBannerView *)bannerView
{
/// Called when an ad request loaded an ad. This is a good opportunity to add this view to the
/// hierarchy if it has not been added yet. If the ad was received as a part of the server-side auto
/// refreshing, you can examine the hasAutoRefreshed property of the view.
NSLog(@"Ad Recived");
}
- (void)adView:(DFPBannerView *)bannerView didFailToReceiveAdWithError:(GADRequestError *)error {
/// Called when an ad request failed. Normally this is because no network connection was available
/// or no ads were available (i.e. no fill). If the error was received as a part of the server-side
/// auto refreshing, you can examine the has AutoRefreshed property of the view.
NSLog(@"adView:didFailToReceiveAdWithError:%@", [error localizedDescription]);
}
- (void)adView:(DFPBannerView *)view willChangeAdSizeTo:(GADAdSize)size{
}
Code Snippet For Interstitial Ad
To implement delegate declare GADInterstitialDelegate.
@property(nonatomic, strong) LinDFPBanner *linInterstitialAd;
In viewDidload write the below code:-
self.linInterstitialAd=[[LinDFPBanner alloc]init];
[self.linInterstitialAd InterstitialAdWithAdUnit:@"xxxxxxxxxxxxxxxxxxxxxxx"];
self.linInterstitialAd.interstitial.delegate=self;
self.linInterstitialAd.PubID=@”lin-xxxxxxxxxx”;
NSDictionary *preferences=@{
@"GeoInfo":@"YES",
@"NetworkInfo":@"YES",
@"DeviceInfo":@"YES"
};
[self.linInterstitialAd setApplicationType:TypeBusiness];
// for custom targeting
[self.linInterstitialAd.LinTarget setValue:@"xxt" forKey:@"yy"];
[self.linInterstitialAd loadRequestLin:[LinDFPBanner LinRequest] interstitial:self.linInterstitialAd.interstitial preferences:preferences andVC:self];
// Check ad is ready in event like button action
if (self.linInterstitialAd.interstitial.isReady) {
[self.linInterstitialAd.interstitial presentFromRootViewController:self];
}
Delegate For Interstitial Ad
- (void)interstitialDidDismissScreen:(DFPInterstitial *)interstitial {
// Called just after dismissing an interstitial and it has animated off the screen.
}
- (void)interstitialDidReceiveAd:(GADInterstitial *)ad{
// Called when an interstitial ad request succeeded. Show it at the next transition point in your
// application such as when transitioning between view controllers.
}
- (void)interstitial:(GADInterstitial *)ad didFailToReceiveAdWithError:(GADRequestError *)error{
// Called when an interstitial ad request completed without an interstitial to
// show. This is common since interstitials are shown sparingly to users.
}
- (void)interstitialWillPresentScreen:(GADInterstitial *)ad{
//Called just before presenting an interstitial. After this method finishes the interstitial will
// animate onto the screen. Use this opportunity to stop animations and save the state of your
// application in case the user leaves while the interstitial is on screen (e.g. to visit the App
// Store from a link on the interstitial).
}
- (void)interstitialWillDismissScreen:(GADInterstitial *)ad{
// Called before the interstitial is to be animated off the screen.
}
- (void)interstitialWillLeaveApplication:(GADInterstitial *)ad{
// Called just before the application will background or terminate because the user clicked on ad
// ad that will launch another application (such as the App Store). The normal
// UIApplicationDelegate methods, like applicationDidEnterBackground:, will be called immediately
// before this.
}