In this tutorial, We will learn about how to integrate Google Mobile SDK to any iOS application. Let’s first get familiar with Google mobile ads. Google mobile ads is an advertising platform for mobile, which will help to generate revenue from the mobile application. So even you are selling your application as free then also you will earn from the application. Google mobile ads provide 4 different kinds of ads.
Google has created documentation for iOS ads integration.
Now, Let’s start creating our sample application with Google mobile ads. If you don’t know about Xcode then I prefer to read our tutorial first to get familiar with Xcode.
Step 1: In Xcode, Create a new project with ‘SingleView Application’. Set a project/product name whatever you want. Select Team name, add Identifier, select ‘Swift Language’ and click next to create a Project. Now it will ask for the location, select the location where you want to save the project.
Step 2: In order to integrate Google mobile SDK you need to Import GoogleMobileAds framework to your project either using CocoaPods or add it manually. If you don’t know about CocoaPods than you should check their official document to learn about CocoaPods.
Using Cocoapods
Open the pod file and add below dependency.
pod ‘Google-Mobile-Ads-SDK’
Add the framework manually
If You want to add Manually then follow below steps
- Download Google Mobile Ads Framework.
- Unzip it.
- Add It in your Project. (Drag and drop the library to your Xcode project).
Additionally, you needs to add following frameworks to your project.
- AdSupport.framework
- AudioToolbox.framework
- AVFoundation.framework
- CoreGraphics.framework
- CoreTelephony.framework
- EventKit.framework
- EventKitUI.framework
- MessageUI.framework
- StoreKit.framework
- SystemConfiguration.framework
Once you complete integrating the framework or SDK, clean your project and build it again. If it builds then you are ready to use Google mobile ads in your iOS application.
Step 3: Now it’s time to import GoogleMobileAds and initialisation. Open the appdelegate.swit file and write below line on top of it.
import GoogleMobileAds
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
GADMobileAds.configure(withApplicationID: “YOUR_ADMOB_APP_ID”)
return true
}
}
GADMobileAds.configure will initialize the Google mobile ads SDK with your app id. You can follow this instruction to learn how to create an app id for google ads. As you can see from the top snippet you must initialise the SDK in ‘didFinishLaunching’ method in AppDelegate.swift controller.
Step 4: Banner Ads
Now we will learn how to show banner ads in the iOS application. Open the controller where you want to show banner ads. On top of the controller, import google mobile ads SDK.
import GoogleMobileAds
Create UIVIew in storyboard & set its Constraint to display ads properly for all device sizes.
Now in ViewController.swift (Controller where you want to show ads) create IBOutlet for GADBannerView type of UIView & link it to Storyboard.
In ViewDidLoad method add below lines. This will create a banner ads and will fetch ad the ad from the google. Sometimes it will show a blank if there is no ads are available for your region. You can use google’s test id to test integration.
Now, Run the project and you will see a nice ad like below image. Please note that for this tutorial we have used test id so it is showing test banner ad.
Step 5: Interstitial Ads:
Showing interstitial ads is almost similar to adding banner ads. Open the controller where you want to show interstitial ads. On top of the controller import google mobile ads SDK.
import GoogleMobileAds
Create a variable for GADInterstitial Ads as below:
Now, Add following code in ViewDidLoad to load interstitial Ad
Unlike banner ads it won’t show ads on load request. It will just load an interstitial ad and it will use a callback to method to inform you ad is ready to show.
/// Tells the delegate an ad request succeeded.
func interstitialDidReceiveAd(_ ad: GADInterstitial) {
print(“interstitialDidReceiveAd”)
}
Now, whenever you are going to show the interstitial ad you need to first check if ad is ready to show or not like below code.
@IBAction func showAd(_ sender: AnyObject) {
…
if interstitial.isReady {
interstitial.present(fromRootViewController: self)
} else {
print(“Ad wasn ‘t ready”)
}
}
You can see the interstitial in full screen like below image. It may be either video or image depends on the setting you set while creating apps on google admob.
Don’t hesitate to contact us if you face any issue. You can also hire iOS developer to implement ads or for your next iOS app idea.
All are excellent types add who provide google mobile ads. One of the best things in this blog is clean and easy-to-understand information on the Google mobile ads. Thanks for your efforts.
Fantastic website! Thank you, I’ve been looking for information about this topic for a long time, and yours is the best I’ve found thus far. Once again, Thank you so much for about how to monetize your ios application with google admob google mobile sdk.