Monetize Your App Using Google Admob

What is AdMob ?

The very first question that comes in your mind is “What is AdMob?”.

Well, AdMob is a Google product that allows you to monetize your app. At present, it’s a part of Firebase package and works on Android and iOS platform.
Google AdMob
It gives you different types of advertisements with different sizes, but we will only focus on banner ads. Below, I have tabulated different banner sizes.

 Standard Banner   320x50
 Large Banner   320x100
 IAB Medium Rectangle   300x250
IAB Full Size Banner
  468x60
 IAB Leaderboard   728x90

In this article, we will create an app in AdMob Web Panel and then use that in iOS Demo Application.

First, visit https://apps.admob.com/ and login with your Google account. Fill all your basic profiling where they ask you about your Name, Country, Address, Time zone, and Currency.

After all the setup, you will have something like this.

something like

Now, click on Monetize New App.

Monetize New App

Pass the name of your app and select platform. For me, it’s iOS.

Monetize New App

Say, my app name is AdMobDemo. Note down its App ID.

Choose Banner Type and and select Text and Image check box. Ideally, refresh rate should be 60 seconds.
Inside Ad unit name, pass some unique value. I would recommend you to give the bundle identifier of your app.

Monetize New App

Note: Bundle Identifier is unique combination for every app.

And, now click on Save,
Monetize New App
In the third step, we will chose skip.
skip

Here, you get the Ad Unit ID which we are going to use in our code. Download Firebase.zip from the given link under Google Mobile Ads SDK.
 
Follow these steps.
  1. Create a Single View Application Project.

  2. In Finder, get Your GoogleMobileAds.framework file and drop it in the project. 

    GoogleMobileAds

  3. As you can see the framework file in your Project Navigator, open Storyboard file and add a UIView in bottom of the View-Controller with the proper constraint. 
  4. Open Storyboard file and add a UIView object in the ViewController scene.

     Add GADBannerView in the File Inspector of that UIView.



  5. Next, we will create a outlet connection of the View.

  6. Next, we will write code for showing the advertisement in that UIView.
    1. <code>    
    2. import UIKit    
    3. import GoogleMobileAds    
    4. class ViewController: UIViewController, GADBannerViewDelegate {    
    5. @IBOutlet weak var adBannerView: GADBannerView!    
    6. override func viewDidLoad() {    
    7. super.viewDidLoad()    
    8. //GAD Request    
    9. let request = GADRequest()    
    10. //For Testing    
    11. request.testDevices = [kGADSimulatorID]    
    12. adBannerView.delegate = self    
    13. //Set AdUnit ID from AdMob Panel    
    14. adBannerView.adUnitID = "ca-app-pub-1384203533772456/7292689720"    
    15. adBannerView.rootViewController = self    
    16. //Set View with the request    
    17. adBannerView.loadRequest(request)    
    18. //Visibility    
    19. adBannerView.hidden = true    
    20. }    
    21. override func didReceiveMemoryWarning() {    
    22. super.didReceiveMemoryWarning()    
    23. // Dispose of any resources that can be recreated.    
    24. }    
    25. func adViewDidReceiveAd(bannerView: GADBannerView!){    
    26. adBannerView.hidden = false    
    27. }    
    28. func adView(bannerView: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!){    
    29. print("Error : \(error.description)")    
    30. }    
    31. }    
    32. </code> 
code

Explanation

First, you import the GoogleMobileAdv framework. And then, move to viewDidLoad() method where we want to initialize the bannerView (adBannerView).

Create an object of GDRequest which we will need further. Then, add a testing device (as, simulator) and set delegate as self so that we can use the delegate methods.Here, we are using two delegate methods : One is when we successfully receive the advertisement and one when it fails to load advertisement.
  1. func adViewDidReceiveAd(bannerView: GADBannerView!)
  2. func adView(bannerView: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!)
Next, set the AdUnit ID to initialize the adv to the banner.

Next, set the root View Controller to the present View Controller. 

output
 That’s it ! You can see the advertisement in the above screenshot.


Similar Articles