Appcoda

Displaying Facebook Ads in Your iOS Apps

Not so long time ago I was asked to integrate Facebook ads in a project. Without having done so before, I prepared myself for a potentially long process, even a little bit painful, and then I got started searching the docs. Against my initial thoughts though, I went through the docs pretty fast, and after a few minutes I had my first Facebook ads up and running!

Facebook ads consist of a nice way to embed advertisements in your apps and make some money out of them if they gain significant visibility. The process to get there (to embed ads) is not complicated at all, as Facebook SDK (an SDK with frameworks made by Facebook) contains an ad-related framework called Audience Network. That framework is pretty much all you need for having ads being served inside an app.

There are various types of ads that Facebook provides for various platforms, but when talking about mobile devices then there are actually three types: Native, Interstitial, and Banners. Here’s a super-quick look at each type:

  • Native ads can be placed anywhere in the app, and the views that they appear to can be customised so they do not distinguish from the rest of the UI. Those views are either manually created views (custom controls that are assigned with an ad’s details), or a view template given by the Audience Network Framework that has certain attributes for customisation. However, a special and interesting case is when you want to display ads in a tableview, where you have two options: Either you create your custom views (in that case custom cells) that will contain the ads and you perform all the related tableview handling manually, or you use some specific classes from the Audience Network Framework (real gems) and you get the tableview handling almost for free. The drawback here is the fact that you lose the option to customise the appearance of the ads, but in several cases that’s not a problem at all.
  • Interstitial ads are actually full screen ads, and as you understand, they hide the entire content of the app when they’re being displayed. Therefore, no need for customisation here regarding the appearance of the ads.
  • Banner ads are small views that usually appear either to the top or to the bottom of the screen, but no customisation is allowed here as well, except for the height of the banner.

We are going to go through all the above cases in this post together, but besides that, I will also show to you how to make the necessary required configuration to the Facebook Developers portal. Actually, I will guide you to the configuration needed to make the app capable of showing ads, and for the rest (providing information, payout info, etc) I will give you just one, ancient advice that’s always true: Read the documentation (also known as RTFM).

You may be wondering why I decided to write a tutorial about Facebook ads when I admit that it’s easy to integrate them in an iOS app. There are two reasons for that: Firstly, as I found almost nothing on the web to get me straight into the point, I considered to be a good idea to give you an immediate how-to guide so you can have ads being served in your app in no time at all. Secondly, all the sample code you can find on Facebook docs is written in Objective-C, so new developers that know about Swift only and not Objective-C will probably encounter some problems to “translate” the code from one programming language to another.

Lastly, and just for the records, keep in mind that the demo project that I’m about to present right next and you can download was made in Xcode 7.3.1 and Swift 2.2. That’s the official version still for both, so don’t start complaining if something doesn’t work in Swift 3.0.

Continue reading my tutorial on Appcoda