How to integrate admob ads in ios app
To integrate AdMob ads into your iOS app, you’ll need to follow these general steps:
- Create a Google AdMob Account:
Go to the Google AdMob website (https://admob.google.com/) and sign up or log in with your Google account. Once logged in, create a new app or select an existing app to integrate AdMob ads.- Add Your App to Google AdMob:
After creating your app in the Google AdMob Dashboard, you need to add your iOS app to the app settings. You’ll need to provide your app’s bundle ID. - Set Up Google Mobile Ads SDK:
Add the Google Mobile Ads SDK to your iOS project using CocoaPods or manually. Using CocoaPods:
Add the following line to yourPodfile
and runpod install
:
pod 'Google-Mobile-Ads-SDK'
Manually:
Download the Google Mobile Ads SDK from the Google AdMob website and follow the instructions to add it to your Xcode project.
56
- Initialize Google Mobile Ads SDK:
In yourAppDelegate
orViewController
, initialize the Mobile Ads SDK with your AdMob app ID.
// Swift
import GoogleMobileAds
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
GADMobileAds.sharedInstance().start(completionHandler: nil)
return true
}
0
- Add Google Ad Components:
Add Google ad components to your view controller’s storyboard or programmatically. Example Banner Ad:
// Swift
import GoogleMobileAds
class ViewController: UIViewController, GADBannerViewDelegate {
var bannerView: GADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
bannerView = GADBannerView(adSize: kGADAdSizeBanner)
bannerView.adUnitID = "YOUR_AD_UNIT_ID"
bannerView.rootViewController = self
bannerView.delegate = self
addBannerViewToView(bannerView)
let request = GADRequest()
bannerView.load(request)
}
func addBannerViewToView(_ bannerView: GADBannerView) {
bannerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bannerView)
view.addConstraints([
NSLayoutConstraint(item: bannerView,
attribute: .bottom,
relatedBy: .equal,
toItem: view.safeAreaLayoutGuide,
attribute: .bottom,
multiplier: 1,
constant: 0),
NSLayoutConstraint(item: bannerView,
attribute: .centerX,
relatedBy: .equal,
toItem: view,
attribute: .centerX,
multiplier: 1,
constant: 0)
])
}
func bannerViewDidReceiveAd(_ bannerView: GADBannerView) {
// Ad loaded successfully
}
func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) {
// Handle ad loading error
}
}
- Load Google Ads:
In your view controller, load the ad into the ad view.
0
// Swift
bannerView.load(request)
- Handle Ad Events:
Implement delegate methods to handle events such as ad loading, clicks, and errors.
// Swift
func bannerViewDidReceiveAd(_ bannerView: GADBannerView) {
// Ad loaded successfully
}
func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) {
// Handle ad loading error
}
- Test Your Integration:
Before releasing your app, make sure to test your Google ads integration using test ads provided by Google. You can use test ad unit IDs to see test ads. - Publish Your App:
After testing, you can publish your app with Google ads integrated.
Remember to comply with Google’s advertising policies and guidelines when integrating and displaying ads in your app.