Skip to content

Instantly share code, notes, and snippets.

@ozgurshn
Created April 6, 2022 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozgurshn/e30e48dba3642594ba2a743ddec19d1a to your computer and use it in GitHub Desktop.
Save ozgurshn/e30e48dba3642594ba2a743ddec19d1a to your computer and use it in GitHub Desktop.
SwiftUI integration for Rewarded Google Ads
//
// RewardedAdView.swift
// ContactAppSwiftUI
//
// Created by ozgur on 4/6/22.
//
import Foundation
import SwiftUI
import GoogleMobileAds
final class RewardedAdView: NSObject, GADFullScreenContentDelegate, ObservableObject {
static let sharedInstance = RewardedAdView()
static let testId = "ca-app-pub-3940256099942544/1712485313"
/// The rewarded video ad.
var rewardedAd: GADRewardedAd?
override init() {
super.init()
loadRewarded()
}
/// reklam yukleme fonksiyonu
func loadRewarded()
{
GADRewardedAd.load(
withAdUnitID: RewardedAdView.testId, request: GADRequest()
) { (ad, error) in
if let error = error {
print("Rewarded ad failed to load with error: \(error.localizedDescription)")
return
}
print("Loading Succeeded")
self.rewardedAd = ad
self.rewardedAd?.fullScreenContentDelegate = self
}
}
/// Reklam gosterme fonksiyonu
func showAd()
{
if let ad = rewardedAd {
let root = UIApplication.shared.windows.first?.rootViewController
ad.present(fromRootViewController: root!) {
let reward = ad.adReward
print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)")
// TODO: burada basarili bisekilde reklam izlenmis artik odul krediyi verebiliriz ve kullanicyi bilgilendirebiliriz
}
}
else {
NSLog("Rewarded Ad wasn't ready")
}
}
// MARK: GADFullScreenContentDelegate
func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Rewarded ad will be presented.")
}
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Rewarded ad dismissed.")
}
/// reklam gosterimi hata aldiysa bu fonksiyon cagirlyior
/// - Parameters:
/// - ad: <#ad description#>
/// - error: <#error description#>
func ad(
_ ad: GADFullScreenPresentingAd,
didFailToPresentFullScreenContentWithError error: Error
) {
print("Rewarded ad failed to present with error: \(error.localizedDescription).")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment