FLINTERS Engineer's Blog

FLINTERSのエンジニアによる技術ブログ

【Swift】Google AdMob Ads SDK in Swift - Simple Tutorial

In this tutorial we will create an advertising banner using the Google Mobile Ads SDK. Tips for reading this tutorial, I would suggest you read this tutorial step by step and I hope you will enjoy it too. Note*, some parts of this tutorial are modified from "Mobile Ads SDK in Objective-C article [3]".

[Requirements]

  1. Xcode version 6 or higher
  2. Mobile target iOS 8
  3. Swift Language
  4. CocoaPods
  5. Google-Mobile-Ads-SDK

Let's create a simple single view application

  1. Create a new Xcode project
  2. Choose a template for your new project -> select "Single View Application" -> Next
  3. Fill your product name and choose Language : Swift
  4. Build and run your new project, the app will show a blank screen.

Let's add AdMob SDK into your application

Note: CocoaPods is the dependency manager for Swift and Objective-C

We will install AdMob SDK by using CocoaPods. There are 2 steps for the installation.

1.Create the Podfile (in the same directory as the example.xcodeproj)

  • File name: Podfile
  • File contents:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '7.0'
pod 'Google-Mobile-Ads-SDK', '~> 7.0'

2.Install AdMob SDK by using Podfile

  • Close all projects
  • Open terminal
  • Go to directory which has Podfile
  • Run
pod install

Let's make a simple banner in your application

1.Open up file "example.xcworkspace" in the same directory as the example.xcodeproj (You will see an additional files in your application: Pods)

f:id:k_chindamaikul:20150804104734p:plain

2.Rebuild and run your project. The application will show a white screen, but now the Ads SDK has included in your application already.

3.Add "GADBannerView" into the Storyboard File:

  • Search for UIView
  • Drag UIView element into your view controller
  • In the Identity inspector: set the properties as below
UIView class: GADBannerView
UIView size: 320x50

f:id:k_chindamaikul:20150804110339p:plain

4.Add reference to the ViewController:

import GoogleMobileAds
@IBOutlet weak var bannerView: GADBannerView!

f:id:k_chindamaikul:20150804114133p:plain

f:id:k_chindamaikul:20150804114327p:plain

5.Load an ads into GADBannerView

  • Add the following code into ViewDidLoad function

f:id:k_chindamaikul:20150804130130p:plain

6.Run an application

f:id:k_chindamaikul:20150804130202p:plain

Reference

  1. Swift (https://developer.apple.com/swift/)
  2. The dependency manager for Swift and Objective-C (https://cocoapods.org)
  3. Mobile Ads SDK for iOS with Objective-C (https://developers.google.com/admob/ios/quick-start?hl=en)