Reputation: 119
Before, I used AdMob's banner ad in an iOS app. Now, I want to show its latest released open app ads. I checked the relevant web page of AdMob (enter link description here), but the examples on this page are all in OC language. I am not familiar with OC language. Can anyone provide guidance in Swift language? Thanks in advance.
Upvotes: 7
Views: 5527
Reputation: 377
Along with Mikrasya and TripleTroop answers and if you need to show open ads from SceneDelegate you can use instruction below:
All code below goes to SceneDelegate.swift
Import GoogleMobileAds:
import GoogleMobileAds
In class definition add GADFullScreenContentDelegate:
class SceneDelegate: UIResponder, UIWindowSceneDelegate, GADFullScreenContentDelegate {
Add variable:
var appOpenAd: GADAppOpenAd?
Add these functions:
func requestAppOpenAd() {
let request = GADRequest()
GADAppOpenAd.load(withAdUnitID: "YOUR_ID",
request: request,
orientation: UIInterfaceOrientation.portrait,
completionHandler: { (appOpenAdIn, _) in
self.appOpenAd = appOpenAdIn
self.appOpenAd?.fullScreenContentDelegate = self
print("Ad is ready")
})
}
func tryToPresentAd() {
if let gOpenAd = self.appOpenAd, let rwc = UIApplication.shared.windows.last?.rootViewController {
gOpenAd.present(fromRootViewController: rwc)
} else {
self.requestAppOpenAd()
}
}
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
requestAppOpenAd()
}
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
requestAppOpenAd()
}
func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Ad did present")
}
And now we can change sceneDidBecomeActive
function to call open ads:
func sceneDidBecomeActive(_ scene: UIScene) {
self.tryToPresentAd()
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}
Upvotes: 4
Reputation: 99
Along with Mikrasya answer, in order to load another ad in case of ad fail or dimiss you gona need to call below functions as well
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
requestAppOpenAd()
}
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
requestAppOpenAd()
}
func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Ad did present")
}
Upvotes: 0
Reputation: 1140
OC code here: https://developers.google.com/admob/ios/app-open-ads
All code below goes to AppDelegate.swift
Import GoogleMobileAds
:
import GoogleMobileAds
In class definition add GADFullScreenContentDelegate
:
class AppDelegate: UIResponder, UIApplicationDelegate, GADFullScreenContentDelegate {
Add two variables:
var appOpenAd: GADAppOpenAd?
var loadTime = Date()
Add these three functions:
func requestAppOpenAd() {
let request = GADRequest()
GADAppOpenAd.load(withAdUnitID: "YOUR_ADUNIT_ID",
request: request,
orientation: UIInterfaceOrientation.portrait,
completionHandler: { (appOpenAdIn, _) in
self.appOpenAd = appOpenAdIn
self.appOpenAd?.fullScreenContentDelegate = self
self.loadTime = Date()
print("Ad is ready")
})
}
func tryToPresentAd() {
if let gOpenAd = self.appOpenAd, let rwc = self.window?.rootViewController, wasLoadTimeLessThanNHoursAgo(thresholdN: 4) {
gOpenAd.present(fromRootViewController: rwc)
} else {
self.requestAppOpenAd()
}
}
func wasLoadTimeLessThanNHoursAgo(thresholdN: Int) -> Bool {
let now = Date()
let timeIntervalBetweenNowAndLoadTime = now.timeIntervalSince(self.loadTime)
let secondsPerHour = 3600.0
let intervalInHours = timeIntervalBetweenNowAndLoadTime / secondsPerHour
return intervalInHours < Double(thresholdN)
}
Finally call tryToPresentAd()
from applicationDidBecomeActive()
:
func applicationDidBecomeActive(_ application: UIApplication) {
self.tryToPresentAd()
}
That's it.
Edit: Added the need to import GoogleMobileAds
Upvotes: 15