This blog includes how to display banner ads for DFP users.For this first user need to signed up as a DFP (Double click for publisher) and then install cocoa pods for Google mobile ads.
pod 'Google-Mobile-Ads-SDK'
We can also include the SDK manually but using pod is the best way.
After that in your view controller use the following properties-
@import GoogleMobileAds;
@interface ViewController ()<GADBannerViewDelegate>{
BOOL _bannerIsVisible;
}
@property (strong, nonatomic) DFPBannerView *bannerView;
@end
After this initialize the banner view in viewdidload-
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_bannerView = [[DFPBannerView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, 50)];
[self.view addSubview:_bannerView];
//set the ad unit id
self.bannerView.adUnitID = @"/31930164/Your-banner-key";
self.bannerView.rootViewController = self;
[self.bannerView loadRequest:[DFPRequest request]];
//set delegate
[self.bannerView setDelegate:self];
}
then use the delegates of banner view-
- (void)adViewDidReceiveAd:(GADBannerView *)bannerView{
if (!_bannerIsVisible)
{
if (bannerView.superview == nil){
[self.view addSubview:bannerView];
}
[UIView beginAnimations:@"animateAdBannerOn" context:NULL];
// Assumes the banner view is just off the bottom of the screen.
bannerView.frame = CGRectOffset(bannerView.frame, 0, -bannerView.frame.size.height);
CGRect frame = self.view.frame;
frame.size.height = bannerView.frame.origin.y;
self.view.frame = frame;
[UIView commitAnimations];
_bannerIsVisible = YES;
}
}
- (void)adView:(GADBannerView *)bannerView didFailToReceiveAdWithError:(GADRequestError *)error{
NSLog(@"Failed to retrieve ad %@",error.description);
if (_bannerIsVisible)
{
[UIView beginAnimations:@"animateAdBannerOff" context:NULL];
// Assumes the banner view is placed at the bottom of the screen.
bannerView.frame = CGRectOffset(bannerView.frame, 0, bannerView.frame.size.height);
CGRect frame = self.view.frame;
frame.size.height = bannerView.frame.origin.y;
self.view.frame = frame;
[UIView commitAnimations];
_bannerIsVisible = NO;
}
}
0 Comment(s)