在 iOS 上从 Google AdMob (ex. AdMob) 迁移到 Yandex 聚合
要在 iOS 上使用 Yandex Ads 聚合替换 Google AdMob (ex. AdMob) 广告,请对您的代码进行以下更改。
集成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
}
}
// 非必要
广告格式
插屏广告
加载广告
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
...
/// 告知代理广告未能呈现全屏内容。
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
print("Ad did fail to present full screen content.")
}
/// 告知代理广告将呈现全屏内容。
func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Ad will present 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")
}
}
激励广告
加载广告
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
...
/// 告知代理广告未能呈现全屏内容。
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
print("Ad did fail to present full screen content.")
}
/// 告知代理广告将呈现全屏内容。
func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Ad will present 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")
}
}
横幅广告
配置
Google AdMob (ex. AdMob)
Yandex Ads
class ViewController: UIViewController {
var bannerView: GADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
// 在这种情况下,我们使用所需的广告尺寸实例化横幅。
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()
// 将 demo-banner-yandex0 替换为实际的广告单元 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)
// 也可以使用常规的自动布局
设置广告回调
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())
这篇文章是否有用?
下一篇