Skip to content

Instantly share code, notes, and snippets.

@antonio081014
Created June 5, 2016 20:53
Show Gist options
  • Save antonio081014/19fb4f376a171da7ed1ed76534025917 to your computer and use it in GitHub Desktop.
Save antonio081014/19fb4f376a171da7ed1ed76534025917 to your computer and use it in GitHub Desktop.
Snippet of code on How to add Facebook Ads Banner to ViewController
//
// ViewController.m
//
// Created by Antonio081014 on 6/4/16.
// Copyright © 2016 Antonio081014.com. All rights reserved.
//
#import "ViewController.h"
@import FBAudienceNetwork;
@interface ViewController () <FBAdViewDelegate>
@property(nonatomic, strong) FBAdView *adView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.adView = [[FBAdView alloc] initWithPlacementID:@"XXX"
adSize:kFBAdSizeHeight50Banner
rootViewController:self];
[FBAdSettings addTestDevice:@"Hash ID for testing on real device purpose."];
self.adView.delegate = self;
NSLog(@"%@", NSStringFromCGRect(self.adView.frame));
self.adView.frame = CGRectMake(0, CGRectGetMinY(self.view.bounds) + self.topLayoutGuide.length, CGRectGetWidth(self.view.bounds), 50);
[self.view addSubview:self.adView];
[self.adView loadAd];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.adView.frame = CGRectMake(0, CGRectGetMinY(self.view.bounds) + self.topLayoutGuide.length, CGRectGetWidth(self.view.bounds), 50);
}
- (void)adView:(FBAdView *)adView didFailWithError:(NSError *)error;
{
NSLog(@"Ad failed to load");
// Add code to hide the ad unit...
// E.g. adView.hidden = YES;
}
- (void)adViewDidLoad:(FBAdView *)adView;
{
NSLog(@"Ad was loaded and ready to be displayed");
// Add code to show the ad unit...
// E.g. adView.hidden = NO;
}
- (void)adViewDidClick:(FBAdView *)adView
{
NSLog(@"The user clicked on the ad and will be taken to its destination.");
}
- (void)adViewDidFinishHandlingClick:(FBAdView *)adView
{
NSLog(@"The user finished to interact with the ad.");
}
- (UIViewController *)viewControllerForPresentingModalView
{
// return the view controller that is currently presenting the ad unit.
return self;
}
@end
//
// ViewController.swift
//
// Created by Antonio081014 on 6/4/16.
// Copyright © 2016 Antonio081014.com. All rights reserved.
//
import UIKit
import FBAudienceNetwork
class ViewController: UIViewController, FBAdViewDelegate {
var adView:FBAdView = FBAdView.init()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
adView = FBAdView.init(placementID: "XXX", adSize: kFBAdSizeHeight50Banner, rootViewController: self)
adView.delegate = self
FBAdSettings.addTestDevice("Hash ID for testing on real device purpose.")
self.view.addSubview(adView)
adView.loadAd()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
adView.frame = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMinY(self.view.bounds) + self.topLayoutGuide.length, CGRectGetWidth(self.view.bounds), 50)
}
func adViewDidLoad(adView: FBAdView) {
print("Ad was loaded and ready to be displayed")
}
func adView(adView: FBAdView, didFailWithError error: NSError) {
print("Ad failed to load")
}
func adViewDidClick(adView: FBAdView) {
print("The user clicked on the ad and will be taken to its destination.")
}
func adViewDidFinishHandlingClick(adView: FBAdView) {
print("The user finished to interact with the ad.")
}
func viewControllerForPresentingModalView() -> UIViewController {
return self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment