Миграция с Google AdMob (ex. AdMob) на медиацию Яндекса на iOS
Чтобы заменить рекламу Google AdMob (ex. AdMob) на медиацию Yandex Ads на iOS, внесите в свой код следующие изменения.
Подключение SDK
Откройте Podfile вашего проекта и добавьте строку в цель вашего приложения:
Google AdMob (ex. AdMob)
Yandex Ads
pod 'Google-Mobile-Ads-SDK'
pod 'YandexMobileAds'
Обновите свой Info.plist
:
Google AdMob (ex. AdMob)
Yandex Ads
<key>GADApplicationIdentifier</key>
<string>ca-app-pub-3940256099942544~1458002511</string>
<key>SKAdNetworkItems</key>
<array>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>cstr6suwn9.skadnetwork</string>
</dict>
.....
<key>SKAdNetworkItems</key>
<array>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>zq492l623r.skadnetwork</string>
</dict>
</array>
Перед загрузкой рекламы инициализируйте библиотеку:
Google AdMob (ex. AdMob)
Yandex Ads
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
GADMobileAds.sharedInstance().start(completionHandler: nil)
return true
}
}
// Не требуется
Форматы объявлений
Interstitial
Загрузка объявления
Google AdMob (ex. AdMob)
Yandex Ads
class ViewController: UIViewController {
private var interstitial: GADInterstitialAd?
override func viewDidLoad() {
super.viewDidLoad()
let request = GADRequest()
GADInterstitialAd.load(withAdUnitID:"ca-app-pub-3940256099942544/4411468910",
request: request,
completionHandler: { [self] ad, error in
if let error = error {
print("Failed to load interstitial ad with error: \(error.localizedDescription)")
return
}
interstitial = ad
}
)
}
}
class ViewController: UIViewController {
var interstitialAd: YMAInterstitialAd?
override func viewDidLoad() {
super.viewDidLoad()
interstitialAd = YMAInterstitialAd(adUnitID: "demo-interstitial-yandex")
interstitialAd?.delegate = self;
let request = YMAAdRequest()
interstitialAd?.load(with: request)
}
}
Установка коллбеков рекламы
Google AdMob (ex. AdMob)
Yandex Ads
interstitial?.fullScreenContentDelegate = self
...
/// Tells the delegate that the ad failed to present full screen content.
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
print("Ad did fail to present full screen content.")
}
/// Tells the delegate that the ad will present full screen content.
func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Ad will present full screen content.")
}
/// Tells the delegate that the ad dismissed full screen content.
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Ad did dismiss full screen content.")
}
self.interstitialAd?.delegate = self;
...
extension ViewController: YMAInterstitialAdDelegate {
func interstitialAdDidLoad(_ interstitialAd: YMAInterstitialAd) {
print("Ad loaded")
}
func interstitialAdDidFail(toLoad interstitialAd: YMAInterstitialAd, error: Error) {
print("Loading failed. Error: \(error)")
}
func interstitialAd(_ interstitialAd: YMAInterstitialAd, didTrackImpressionWith impressionData: YMAImpressionData?) {
print("Ad imprassion was tracked.")
}
func interstitialAdDidClick(_ interstitialAd: YMAInterstitialAd) {
print("Ad clicked")
}
func interstitialAdDidFail(toPresent interstitialAd: YMAInterstitialAd, error: Error) {
print("Failed to present interstitial. Error: \(error)")
}
func interstitialAdWillAppear(_ interstitialAd: YMAInterstitialAd) {
print("Interstitial ad will appear")
}
func interstitialAdDidDisappear(_ interstitialAd: YMAInterstitialAd) {
print("Interstitial ad did disappear")
}
}
Показать объявление
Google AdMob (ex. AdMob)
Yandex Ads
@IBAction func doSomething(_ sender: Any) {
if interstitial != nil {
interstitial.present(fromRootViewController: self)
} else {
print("Ad wasn't ready")
}
}
@IBAction func doSomething(_ sender: Any) {
if interstitialAd?.loaded ?? false {
interstitialAd?.present(from: self)
} else {
print("Ad wasn't ready")
}
}
Rewarded
Загрузка объявления
Google AdMob (ex. AdMob)
Yandex Ads
class ViewController: UIViewController {
private var rewardedAd: GADRewardedAd?
func loadRewardedAd() {
let request = GADRequest()
GADRewardedAd.load(withAdUnitID:"ca-app-pub-3940256099942544/1712485313",
request: request,
completionHandler: { [self] ad, error in
if let error = error {
print("Failed to load rewarded ad with error: \(error.localizedDescription)")
return
}
rewardedAd = ad
print("Rewarded ad loaded.")
}
)
}
}
class ViewController: UIViewController {
var rewardedAd: YMARewardedAd?
override func viewDidLoad() {
super.viewDidLoad()
rewardedAd = YMARewardedAd(adUnitID: "demo-rewarded-yandex")
rewardedAd?.delegate = self
let request = YMAAdRequest()
rewardedAd?.load(with: request)
}
}
Установка коллбеков рекламы
Google AdMob (ex. AdMob)
Yandex Ads
rewardedAd?.fullScreenContentDelegate = self
...
/// Tells the delegate that the ad failed to present full screen content.
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
print("Ad did fail to present full screen content.")
}
/// Tells the delegate that the ad will present full screen content.
func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Ad will present full screen content.")
}
/// Tells the delegate that the ad dismissed full screen content.
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Ad did dismiss full screen content.")
}
rewardedAd?.delegate = self
...
extension ViewController: YMARewardedAdDelegate {
func rewardedAdDidLoad(_ rewardedAd: YMARewardedAd) {
print("Rewarded ad loaded")
}
func rewardedAdDidFail(toLoad rewardedAd: YMARewardedAd, error: Error) {
print("Loading failed. Error: %@", error)
}
func rewardedAd(_ rewardedAd: YMARewardedAd, didReward reward: YMAReward) {
print("The user earned the reward")
}
func rewardedAdDidFail(toPresent rewardedAd: YMARewardedAd, error: Error) {
print("Failed to present rewarded ad. Error: %@", error)
}
func rewardedAdDidClick(_ rewardedAd: YMARewardedAd) {
print("Ad clicked")
}
func rewardedAd(_ rewardedAd: YMARewardedAd, didTrackImpressionWith impressionData: YMAImpressionData?) {
print("Ad imprassion was tracked.")
}
func rewardedAdWillAppear(_ rewardedAd: YMARewardedAd) {
print("Rewarded ad will appear")
}
func rewardedAdDidDisappear(_ rewardedAd: YMARewardedAd) {
print("Rewarded ad did disappear")
}
}
Показать объявление
Google AdMob (ex. AdMob)
Yandex Ads
@IBAction func doSomething(_ sender: Any) {
if interstitial != nil {
interstitial.present(fromRootViewController: self)
} else {
print("Ad wasn't ready")
}
}
@IBAction func doSomething(_ sender: Any) {
if rewardedAd?.loaded ?? false {
rewardedAd?.present(from: self)
} else {
print("Ad wasn't ready")
}
}
Banner
Конфигурация
Google AdMob (ex. AdMob)
Yandex Ads
class ViewController: UIViewController {
var bannerView: GADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
// In this case, we instantiate the banner with desired ad size.
bannerView = GADBannerView(adSize: GADAdSizeBanner)
bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
bannerView.rootViewController = self
}
}
class ViewController: UIViewController {
var bannerView: YMAAdView!
override func viewDidLoad() {
super.viewDidLoad()
// Replace demo demo-banner-yandex0 with actual Ad Unit ID
let adSize = YMAAdSize.stickySize(withContainerWidth: view.frame.width)
bannerView = YMAAdView(adUnitID: "demo-banner-yandex", adSize: adSize)
}
}
Отображение объявления
Google AdMob (ex. AdMob)
Yandex Ads
bannerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bannerView)
view.addConstraints(
[NSLayoutConstraint(item: bannerView,
attribute: .bottom,
relatedBy: .equal,
toItem: bottomLayoutGuide,
attribute: .top,
multiplier: 1,
constant: 0),
NSLayoutConstraint(item: bannerView,
attribute: .centerX,
relatedBy: .equal,
toItem: view,
attribute: .centerX,
multiplier: 1,
constant: 0)
])
view.addSubview(bannerView)
bannerView.displayAtBottom(in: view)
// also possible to use regular autolayout
Установка коллбеков рекламы
Google AdMob (ex. AdMob)
Yandex Ads
bannerView.delegate = self
...
func bannerViewDidReceiveAd(_ bannerView: GADBannerView) {
print("bannerViewDidReceiveAd")
}
func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) {
print("bannerView:didFailToReceiveAdWithError: \(error.localizedDescription)")
}
func bannerViewDidRecordImpression(_ bannerView: GADBannerView) {
print("bannerViewDidRecordImpression")
}
func bannerViewWillPresentScreen(_ bannerView: GADBannerView) {
print("bannerViewWillPresentScreen")
}
func bannerViewWillDismissScreen(_ bannerView: GADBannerView) {
print("bannerViewWillDIsmissScreen")
}
func bannerViewDidDismissScreen(_ bannerView: GADBannerView) {
print("bannerViewDidDismissScreen")
}
bannerView.delegate = self
...
extension BannerViewController: YMAAdViewDelegate {
func adViewDidLoad(_ adView: YMAAdView) {
print("Ad loaded")
}
func adViewDidFailLoading(_ adView: YMAAdView, error: Error) {
print("Ad failed loading. Error: \(error)")
}
func adViewDidClick(_ adView: YMAAdView) {
print("Ad clicked")
}
func adView(_ adView: YMAAdView, didTrackImpressionWith impressionData: YMAImpressionData?) {
print("Ad imprassion was tracked")
}
}
Загрузка объявления
Google AdMob (ex. AdMob)
Yandex Ads
bannerView.load(GADRequest())
bannerView.loadAd(with: YMAAdRequest())
Была ли статья полезна?
Предыдущая
Следующая