Google AdMob in Swift
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
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)
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
Reference
- Swift (https://developer.apple.com/swift/)
- The dependency manager for Swift and Objective-C (https://cocoapods.org)
- Mobile Ads SDK for iOS with Objective-C (https://developers.google.com/admob/ios/quick-start?hl=en)
Comments
Post a Comment