Created
April 6, 2022 07:22
-
-
Save ozgurshn/e30e48dba3642594ba2a743ddec19d1a to your computer and use it in GitHub Desktop.
SwiftUI integration for Rewarded Google Ads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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