Thursday, March 14, 2019

Integrating Ad Banner in iOS App

I want to show an ad banner at the bottom of the screen in my app like below.

1. Create AdMob account


Go to https://apps.admob.com/signup/ and follow the instructions. Note that AdMob requires Google Adsense account.

AdMob provides SDK for developers to integrate an ad banner (GADBanerView) into their apps. It has many other features like showing only the most payable ads.

2. Download AdMob's SDK and add it to the project


- Download the zip file from here https://developers.google.com/admob/ios/download
- Extract it to a directory, for example: /Volumes/Data/Workspace/Frameworks
- Add it to the project in Xcode by following the instruction here

3. Initialize Mobile Ads SDK


Call configure(withApplicationID:) method in AppDelegate.swift as following:

import GoogleMobileAds

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        GADMobileAds.configure(withApplicationID: "ca-app-pub-3940256099942544~1458002511")
        return true
    }
}
 

The app ID is a test app ID created by AdMob for public use during development. Using the production app ID for development might cause the AdMob account suspended.

4. Update Info.plist file


Open Info.plist file and add a new key, GADApplicationIdentifier , with the string value of the AdMob (test) App ID under Information Property List. It's required by AdMob's SDK.


5. Add GADBannerView to the view hierarchy


In Interface Builder, drag a UIView from the Object Library and add it to the storyboard. Then, open the Identity Inspector and change the Class setting under Custom Class to GADBannerView. Here is the list of the standard banner sizes. I chose 320x50.


6. Configure GADBannerView's properties


Create an outlet named adBanner to connect the GADBannerView in the storyboard with the ViewController. Then, in the ViewController's viewDidLoad() method, initialize two required properties:
- rootViewController: the view controller used to present an overlay when the ad is clicked. Usually, it's set to the view controller that contains the GADBannerView.
- adUnitID: obtain it while registering AdMob account. It identifies a banner view to display in the app.

adBanner.rootViewController = self
adBanner.adUnitID = "ca-app-pub-3940256099942544/2934735716" // test ID

7. Load an ad


Once the banner is placed and its properties are set, it's time to load the ad. Call the load() method in the viewDidLoad():
adBanner.rootViewController = self
adBanner.adUnitID = "ca-app-pub-3940256099942544/2934735716"
adBanner.load(GADRequest())

8. Ad event (optional)


Through the use of GADBannerViewDelegate, we can listen for lifecycle events such as when the ad is closed or the user leaves the app.



FAQs


> Who pay for the ad click or view to the app developer?
- App developers get paid through Google Adsense
- App developers use Google AdMob (a mobile ad platform) to integrate ads from Google advertisers into their apps. AdMob provides iOS or Android SDKs to the developers to do so. AdMob has useful features like showing only the most payable Ads.

> Google Adsense
- Earn money by embedding an ad banner in a blog or web site.
- You don't need to specify the payment method until the your earning has reached the threshold ($100)

> Google AdMob
- Provide SDKs (many types of banners) for mobile apps


References


https://developers.google.com/admob/ios/quick-start
https://developers.google.com/admob/ios/banner


No comments:

Post a Comment