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 ".
Let's create a simple single view application
- Create a new Xcode project
- Choose a template for your new project -> select "Single View Application" -> Next
- Fill your product name and choose Language : Swift
- Build and run your new project, the app will show a blank screen.
Let's add AdMob SDK into your application
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
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)
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
4.Add reference to the ViewController:
import GoogleMobileAds @IBOutlet weak var bannerView: GADBannerView!
5.Load an ads into GADBannerView
- Add the following code into ViewDidLoad function
6.Run an application