Facebook Twitter Gplus LinkedIn RSS
 
 
Home » Blog » Integrating iAds & Google AdMob in Swift

Integrating iAds & Google AdMob in Swift

A long time ago, I attempted to implement advertisements in my Web Apps. This was way before around when Google purchased AdSense and the technology was relatively new. I tried to implement AdMob/iAd failover, and got banned for it. I eventually just used Apple’s iAds instead since it was already built into iOS, and could be invoked with a single one line of code if all you need is a banner at the bottom of the screen.

Times have changed and Apple/Google have relaxed the rules and play better with each other. A number of techniques have cropped up to toggle between multiple ad services. There are some nice scripts out there that invoke AdMob ads when iAds are not available, and vice versa. These involve becoming an iAd Delegate and controlling what is displayed and when. This works pretty well, but can be rather complex. You have to put a lot of intelligence in your code to make sure you aren’t breaking the rules, like requesting an ad more than every 30 seconds, or risk having your account banned. Trust me… it happened to me.

I’ve found a better way though. AdMob has the ability to serve ads from 3rd parties, even Apple iAds!

All you need to do is implement AdMob into your App, then tell Google that you want both AdMob and iAd as potential sources, and then profit. You can even set your own “weights” on each source, if you prefer one service over another. Furthermore, you can add other ad sources and really jump around if you’re inclined. Heck, if you wanted to create your own banner ads for your own apps, you can inject those within the cycle too! AdMob is pretty dang cool.

So here is how you do it:

  1. Go sign up for an AdMob account. I won’t walk you through how to create a password.
  2. Once you have an account validated via email, login and click on Monetize. Fill out the form shown below with your specific App information. You will want to copy and paste the Ad unit ID information that you are given. You will need this later for your code.
    admobmont
  3. On the AdMob website, find your App that you just created, and click on the “sources” link (see screenshot). On this page, you can choose from dozens of different Ad sources. Find iAd, and add it to the list. You do not need to provide any credential information.
    sources
  4. Download the SDK from Google’s site.
  5. Unzip the SDK and import it into Xcode. The easiest way to do this is to click on the + sign on your project’s main page. Go ahead and add the iAd framework while you are here.
    frameworks
  6. You’ll notice in the screenshot there is also a libAdapterIAd.a file. This is an Objective-C linker that lets AdMob speak with the iAd network. Download and add this to your project as well.
  7. Now you need to tell Xcode to use Obj-C to link this particular file to your Swift project. Click on the blue icon that represents your project, then click on Build Settings. From here, find the “other linker” section. Add “-ObjC” (minus the quotes) to both the Debug and Release options as shown.
    otherlinker
  8. Let’s import the libraries into our code by adding this at the top under the other import statements:
    1
    2
    
    import iAd
    import GoogleMobileAds
    import iAd
    import GoogleMobileAds
  9. We need to declare our ViewController a GADBannerViewDelegate at the top:
    1
    
    class ViewController: UIViewController, GADBannerViewDelegate {
    class ViewController: UIViewController, GADBannerViewDelegate {
  10. Inside the ViewController, add the following code to declare our banner:
    1
    
    var UIGAd = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
    var UIGAd = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)

    There are also other sizes available, such as kGADAdSizeSmartBannerLandscape, but my particular App doesn’t support that orientation. If yours does, add some if/else logic for rotation and startup.

  11. Add the following to the viewDidLoad() method.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    // Create Transparent Frame
    UIGAd.alpha = 0
    UIGAd.frame.origin.y = UIScreen.mainScreen().bounds.height - UIGAd.frame.height
    UIGAd.adUnitId = ""
    UIGAd.delegate = self
    UIGAd.rootViewController = self
     
    // Animate it becoming transparent
    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(1)
    UIGAd.alpha = 1
    UIView.commitAnnimations()
     
    // Submit request for an Ad
    let request: GADRequest = GADRequest()
    request.testDevices = [""]
    UIGAd.loadRequest(request)
     
    // Show Ad
    view.addSubview(UIGAd)
    // Create Transparent Frame
    UIGAd.alpha = 0
    UIGAd.frame.origin.y = UIScreen.mainScreen().bounds.height - UIGAd.frame.height
    UIGAd.adUnitId = ""
    UIGAd.delegate = self
    UIGAd.rootViewController = self
    
    // Animate it becoming transparent
    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(1)
    UIGAd.alpha = 1
    UIView.commitAnnimations()
    
    // Submit request for an Ad
    let request: GADRequest = GADRequest()
    request.testDevices = [""]
    UIGAd.loadRequest(request)
    
    // Show Ad
    view.addSubview(UIGAd)

    Let’s review the above code. The first five lines of code that begin with UIGAd is merely creating a view, placing it at the bottom of the screen. The most important part here is the UIGAd.adUnitId. You MUST set this to the Ad Unit ID that I mentioned that you remember in step 2. It looks something like “ca-app-pub-2u03284890239489230890”. If you don’t get that number right, you will display test ads fine. But once your App is on the store, it will show nothing. That would suck!

    The next four lines of code are merely setting up a 1 second animation so the banner fades nicely into view. It isn’t needed but I like the effect.

    The next three lines of code submit a request for an Ad to the AdMob SDK. Remember, this SDK and the linker are handling what ad to display, so you really don’t need to do much for iAd. Notice that we create an array of request.testDevices. If you try to run your app with this empty, you will get an error in Xcode telling you to add a device ID to this array to test. Add it… I can’t tell you what your device ID is. Keep in mind it is against the rules to use real Ads to test your App, so you must put in your device ID to avoid getting banned.

    It is also worth noting that I used viewDidLoad(). You may wish to use viewDidAppear() or viewWillAppear() depending on how your App is setup. For example, if you have some ViewControllers that won’t be displaying Ads, it is wise to use viewDidAppear and viewDidDisappear to toggle this view into and out of sight.

  12. Now go ahead and try your App on all the different simulator devices and your device. Make sure it looks good.

    ios-quickstart-15

About the Author: Sprawl

Stephen Russell is a Mobile App developer and all around IT geek that spends his days running data centers and his nights coding. This site is the go to place for all of zSprawl's work and the infamous development blog. In his free time, he enjoys tinkering with web code, playing video games, and otherwise plotting to take over the Internets.

 

2 Responses

  1. Will I need to implement a timer to reload ads every 30-40 seconds, or will implementing my ads like this auto-load new ads?

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2012 zSprawl's zApps

Fork me on GitHub