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.
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.
Now, click on Monetize New App.
Pass the name of your app and select platform. For me, it’s iOS.
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.
Note: Bundle Identifier is unique combination for every app.
And, now click on Save,
In the third step, we will chose 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.
- Create a Single View Application Project.
- In Finder, get Your GoogleMobileAds.framework file and drop it in the project.
- 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.
- Open Storyboard file and add a UIView object in the ViewController scene.
Add GADBannerView in the File Inspector of that UIView.
- Next, we will create a outlet connection of the View.
- Next, we will write code for showing the advertisement in that UIView.
- <code>
- import UIKit
- import GoogleMobileAds
- class ViewController: UIViewController, GADBannerViewDelegate {
- @IBOutlet weak var adBannerView: GADBannerView!
- override func viewDidLoad() {
- super.viewDidLoad()
-
- let request = GADRequest()
-
- request.testDevices = [kGADSimulatorID]
- adBannerView.delegate = self
-
- adBannerView.adUnitID = "ca-app-pub-1384203533772456/7292689720"
- adBannerView.rootViewController = self
-
- adBannerView.loadRequest(request)
-
- adBannerView.hidden = true
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
-
- }
- func adViewDidReceiveAd(bannerView: GADBannerView!){
- adBannerView.hidden = false
- }
- func adView(bannerView: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!){
- print("Error : \(error.description)")
- }
- }
- </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.
- func adViewDidReceiveAd(bannerView: GADBannerView!)
- 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.
That’s it ! You can see the advertisement in the above screenshot.