From 48b1f028f7c996a74bd7c6f96ec2e2baee4f8ed2 Mon Sep 17 00:00:00 2001 From: amitnahmani Date: Mon, 11 Nov 2024 14:32:33 +0200 Subject: [PATCH] support madu rewarded --- .../Base.lproj/Main.storyboard | 42 ++-- .../DemoBannerAdDelegate.swift | 2 +- .../DemoInterstitialAdDelegate.swift | 6 +- .../DemoRewardedVideoAdDelegate.swift | 67 ++++--- .../DemoViewController.swift | 185 ++++++++++-------- 5 files changed, 171 insertions(+), 131 deletions(-) diff --git a/iOS/Swift/IronSourceSwiftDemoApp/Base.lproj/Main.storyboard b/iOS/Swift/IronSourceSwiftDemoApp/Base.lproj/Main.storyboard index 69785763..67287bdf 100644 --- a/iOS/Swift/IronSourceSwiftDemoApp/Base.lproj/Main.storyboard +++ b/iOS/Swift/IronSourceSwiftDemoApp/Base.lproj/Main.storyboard @@ -37,31 +37,48 @@ - + - + - + @@ -147,7 +164,7 @@ - + @@ -174,10 +191,11 @@ - - - - + + + + + diff --git a/iOS/Swift/IronSourceSwiftDemoApp/DemoBannerAdDelegate.swift b/iOS/Swift/IronSourceSwiftDemoApp/DemoBannerAdDelegate.swift index 179d1ff6..1540b57b 100644 --- a/iOS/Swift/IronSourceSwiftDemoApp/DemoBannerAdDelegate.swift +++ b/iOS/Swift/IronSourceSwiftDemoApp/DemoBannerAdDelegate.swift @@ -22,7 +22,7 @@ class DemoBannerAdDelegate: NSObject, LPMBannerAdViewDelegate { */ func didLoadAd(with adInfo: LPMAdInfo) { logCallbackName(string: "\(#function) adInfo = \(String(describing:adInfo.self))") - self.delegate?.setButtonEnablement(ButtonIdentifiers.loadBannerButtonIdentifier, enable: false) + self.delegate?.setButtonEnablement(ButtonIdentifiers.loadBannerAdButtonIdentifier, enable: false) } /** diff --git a/iOS/Swift/IronSourceSwiftDemoApp/DemoInterstitialAdDelegate.swift b/iOS/Swift/IronSourceSwiftDemoApp/DemoInterstitialAdDelegate.swift index 588de2c3..8d9d1666 100644 --- a/iOS/Swift/IronSourceSwiftDemoApp/DemoInterstitialAdDelegate.swift +++ b/iOS/Swift/IronSourceSwiftDemoApp/DemoInterstitialAdDelegate.swift @@ -22,7 +22,7 @@ class DemoInterstitialAdDelegate: NSObject, LPMInterstitialAdDelegate { */ func didLoadAd(with adInfo: LPMAdInfo) { logCallbackName(string: "\(#function) adInfo = \(String(describing:adInfo.self))") - self.delegate?.setButtonEnablement(ButtonIdentifiers.showInterstitialButtonIdentifier, enable: true) + self.delegate?.setButtonEnablement(ButtonIdentifiers.showInterstitialAdButtonIdentifier, enable: true) } /** @@ -31,7 +31,7 @@ class DemoInterstitialAdDelegate: NSObject, LPMInterstitialAdDelegate { */ func didFailToLoadAd(withAdUnitId adUnitId: String, error: Error) { logCallbackName(string: "\(#function) error = \(String(describing:error.self))") - self.delegate?.setButtonEnablement(ButtonIdentifiers.showInterstitialButtonIdentifier, enable: false) + self.delegate?.setButtonEnablement(ButtonIdentifiers.showInterstitialAdButtonIdentifier, enable: false) } /** @@ -50,7 +50,7 @@ class DemoInterstitialAdDelegate: NSObject, LPMInterstitialAdDelegate { */ func didDisplayAd(with adInfo: LPMAdInfo) { logCallbackName(string: "\(#function) adInfo = \(String(describing:adInfo.self))") - self.delegate?.setButtonEnablement(ButtonIdentifiers.showInterstitialButtonIdentifier, enable: false) + self.delegate?.setButtonEnablement(ButtonIdentifiers.showInterstitialAdButtonIdentifier, enable: false) } /** diff --git a/iOS/Swift/IronSourceSwiftDemoApp/DemoRewardedVideoAdDelegate.swift b/iOS/Swift/IronSourceSwiftDemoApp/DemoRewardedVideoAdDelegate.swift index 56976811..7e32853a 100644 --- a/iOS/Swift/IronSourceSwiftDemoApp/DemoRewardedVideoAdDelegate.swift +++ b/iOS/Swift/IronSourceSwiftDemoApp/DemoRewardedVideoAdDelegate.swift @@ -8,79 +8,86 @@ import Foundation import IronSource -class DemoRewardedVideoAdDelegate: NSObject, LevelPlayRewardedVideoDelegate { - +class DemoRewardedVideoAdDelegate: NSObject, LPMRewardedAdDelegate { + weak var delegate: DemoViewControllerDelegate? init(delegate: DemoViewControllerDelegate!) { self.delegate = delegate } - + /** - Called after a rewarded video has changed its availability to true. + Called after a rewarded ad has been loaded @param adInfo The info of the ad. */ - func hasAvailableAd(with adInfo: ISAdInfo!) { + func didLoadAd(with adInfo: LPMAdInfo) { logCallbackName(string: "\(#function) adInfo = \(String(describing:adInfo.self))") - self.delegate?.setButtonEnablement(ButtonIdentifiers.showRewardedVideoButtonIdentifier, enable: true) + self.delegate?.setButtonEnablement(ButtonIdentifiers.showRewardedAdButtonIdentifier, enable: true) } /** - Called after a rewarded video has changed its availability to false. + Called after a rewarded ad has attempted to load but failed. + @param error The reason for the error */ - func hasNoAvailableAd() { - logCallbackName() - self.delegate?.setButtonEnablement(ButtonIdentifiers.showRewardedVideoButtonIdentifier, enable: false) + func didFailToLoadAd(withAdUnitId adUnitId: String, error: Error) { + logCallbackName(string: "\(#function) error = \(String(describing:error.self))") + self.delegate?.setButtonEnablement(ButtonIdentifiers.showRewardedAdButtonIdentifier, enable: false) + } + + /** + Called after the ad info is updated. Available when another rewarded ad has loaded, and includes a higher CPM/Rate + @param adInfo The info of the ad + */ + func didChangeAdInfo(_ adInfo: LPMAdInfo) { + logCallbackName(string: "\(#function) adInfo = \(String(describing:adInfo.self))") } /** - Called after a rewarded video has been opened. + Called after a rewarded ad has been opened. + This is the indication for impression. @param adInfo The info of the ad. */ - func didOpen(with adInfo: ISAdInfo!) { + func didDisplayAd(with adInfo: LPMAdInfo) { logCallbackName(string: "\(#function) adInfo = \(String(describing:adInfo.self))") - self.delegate?.setButtonEnablement(ButtonIdentifiers.showRewardedVideoButtonIdentifier, enable: true) + self.delegate?.setButtonEnablement(ButtonIdentifiers.showRewardedAdButtonIdentifier, enable: false) } /** - Called after a rewarded video has attempted to show but failed. + Called after a rewarded ad has attempted to show but failed. @param error The reason for the error. @param adInfo The info of the ad. */ - func didFailToShowWithError(_ error: Error!, andAdInfo adInfo: ISAdInfo!) { + func didFailToDisplayAd(with adInfo: LPMAdInfo, error: Error) { logCallbackName(string: "\(#function) error = \(String(describing:error.self)) | adInfo = \(String(describing:adInfo.self))") } /** - Called after a rewarded video has been clicked. - This callback is not supported by all networks, and we recommend using it - only if it's supported by all networks you included in your build - @param placementInfo An object that contains the placement's reward name and amount. + Called after a rewarded ad has been viewed completely and the user is eligible for a reward. + @param reward An object that contains the reward name and amount. @param adInfo The info of the ad. - */ - func didClick(_ placementInfo: ISPlacementInfo!, with adInfo: ISAdInfo!) { - logCallbackName(string: "\(#function) placement = \(String(describing:placementInfo.self)) | adInfo = \(String(describing:adInfo.self))") + */ + func didRewardAd(with adInfo: LPMAdInfo, reward: LPMReward) { + logCallbackName(string: "\(#function) reward = \(String(describing:reward.self)) | adInfo = \(String(describing:adInfo.self))") + self.delegate?.setRewardInfo(reward) } /** - Called after a rewarded video has been viewed completely and the user is eligible for a reward. - @param placementInfo An object that contains the placement's reward name and amount. + Called after a rewarded ad has been clicked. @param adInfo The info of the ad. */ - func didReceiveReward(forPlacement placementInfo: ISPlacementInfo!, with adInfo: ISAdInfo!) { - logCallbackName(string: "\(#function) placement = \(String(describing:placementInfo.self)) | adInfo = \(String(describing:adInfo.self))") - self.delegate?.setPlacementInfo(placementInfo) + func didClickAd(with adInfo: LPMAdInfo) { + logCallbackName(string: "\(#function) adInfo = \(String(describing:adInfo.self))") } /** - Called after a rewarded video has been dismissed. + Called after a rewarded ad has been dismissed. @param adInfo The info of the ad. */ - func didClose(with adInfo: ISAdInfo!) { + func didCloseAd(with adInfo: LPMAdInfo) { logCallbackName(string: "\(#function) adInfo = \(String(describing:adInfo.self))") self.delegate?.showVideoRewardMessage() } - + //MARK: Helper Method func logCallbackName(string: String = #function) { diff --git a/iOS/Swift/IronSourceSwiftDemoApp/DemoViewController.swift b/iOS/Swift/IronSourceSwiftDemoApp/DemoViewController.swift index 67da5ca6..ae8bdb46 100644 --- a/iOS/Swift/IronSourceSwiftDemoApp/DemoViewController.swift +++ b/iOS/Swift/IronSourceSwiftDemoApp/DemoViewController.swift @@ -14,14 +14,17 @@ import IronSource let appKey = "8545d445" // Replace with your ad unit ids as available in the LevelPlay dashboard -let interstitialAdUnitId = "wmgt0712uuux8ju4" -let bannerAdUnitId = "iep3rxsyp9na3rw8" +let interstitialAdAdUnitId = "wmgt0712uuux8ju4" +let bannerAdAdUnitId = "iep3rxsyp9na3rw8" +let rewardedAdAdUnitId = "qwouvdrkuwivay5q" + enum ButtonIdentifiers : Int { - case showRewardedVideoButtonIdentifier - case loadInterstitialButtonIdentifier - case showInterstitialButtonIdentifier - case loadBannerButtonIdentifier + case loadRewardedAdButtonIdentifier + case showRewardedAdButtonIdentifier + case loadInterstitialAdButtonIdentifier + case showInterstitialAdButtonIdentifier + case loadBannerAdButtonIdentifier } protocol DemoViewControllerDelegate: NSObjectProtocol { @@ -29,7 +32,7 @@ protocol DemoViewControllerDelegate: NSObjectProtocol { _ buttonIdentifier: ButtonIdentifiers, enable: Bool ) - func setPlacementInfo(_ placementInfo: ISPlacementInfo?) + func setRewardInfo(_ reward: LPMReward?) func showVideoRewardMessage() } @@ -37,16 +40,18 @@ class DemoViewController: UIViewController, DemoViewControllerDelegate { //MARK: IBOutlets - @IBOutlet weak var showRewardedVideoButton: UIButton! - var rewardedVideoDelegate: DemoRewardedVideoAdDelegate! = nil - var rewardedVideoPlacementInfo: ISPlacementInfo! = nil - - @IBOutlet weak var loadInterstitialButton: UIButton! - @IBOutlet weak var showInterstitialButton: UIButton! + @IBOutlet weak var loadRewardedAdButton: UIButton! + @IBOutlet weak var showRewardedAdButton: UIButton! + var rewardedAdDelegate: DemoRewardedVideoAdDelegate! = nil + var reward: LPMReward! = nil + var rewardedAd: LPMRewardedAd! = nil + + @IBOutlet weak var loadInterstitialAdButton: UIButton! + @IBOutlet weak var showInterstitialAdButton: UIButton! var interstitialAdDelegate: DemoInterstitialAdDelegate! = nil var interstitialAd: LPMInterstitialAd! = nil - @IBOutlet weak var loadBannerButton: UIButton! + @IBOutlet weak var loadBannerAdButton: UIButton! var bannerAdViewDelegate: DemoBannerAdDelegate! = nil var bannerAd: LPMBannerAdView! = nil var bannerSize: LPMAdSize! = nil @@ -76,7 +81,7 @@ class DemoViewController: UIViewController, DemoViewControllerDelegate { func setupUI() { self.versionLabel.text = String(format: "sdk version %@", IronSource.sdkVersion()); - let buttons = [self.showRewardedVideoButton, self.loadInterstitialButton, self.showInterstitialButton, self.loadBannerButton] + let buttons = [self.loadRewardedAdButton, self.showRewardedAdButton, self.loadInterstitialAdButton, self.showInterstitialAdButton, self.loadBannerAdButton] for button in buttons { button?.layer.cornerRadius = 17.0 @@ -93,26 +98,16 @@ class DemoViewController: UIViewController, DemoViewControllerDelegate { // Remove it before going live! ISIntegrationHelper.validateIntegration() #endif - - // Before initializing any of our products (Rewarded video, Interstitial or Banner) you must set - // their delegates. Take a look at each of these delegates method and you will see that they each implement a product - // protocol. This is our way of letting you know what's going on, and if you don't set the delegates - // we will not be able to communicate with you. - // We're passing 'self' to our delegates because we want - // to be able to enable/disable buttons to match ad availability. - rewardedVideoDelegate = .init(delegate: self) - IronSource.setLevelPlayRewardedVideoDelegate(rewardedVideoDelegate) - + + // Register a listener for all impressions within the current session impressionDataDelegate = .init() IronSource.add(self.impressionDataDelegate) // After setting the delegates you can go ahead and initialize the SDK. // Once the iniitaliztion callback is return you can start loading your ads - // Init the SDK when implementing the Multiple Ad Units Interstitial and Banner API, and Rewarded using legacy APIs - self.logMethodName(string: "init ironSource SDK with appKey: \(appKey)") + self.logMethodName(string: "init LevePlay SDK with appKey: \(appKey)") let requestBuilder = LPMInitRequestBuilder(appKey: appKey) - .withLegacyAdFormats([IS_REWARDED_VIDEO]) let initRequest = requestBuilder.build() LevelPlay.initWith(initRequest) { config, error in @@ -122,6 +117,7 @@ class DemoViewController: UIViewController, DemoViewControllerDelegate { return } self.logMethodName(string: "sdk initialization succeeded") + self.createRewardedAd() self.createInterstititalAd() self.createBannerAd() } @@ -133,11 +129,11 @@ class DemoViewController: UIViewController, DemoViewControllerDelegate { //MARK: Interstitial Methods func createInterstititalAd() { - self.interstitialAd = LPMInterstitialAd(adUnitId: interstitialAdUnitId) + self.interstitialAd = LPMInterstitialAd(adUnitId: interstitialAdAdUnitId) interstitialAdDelegate = .init(delegate: self) self.interstitialAd.setDelegate(interstitialAdDelegate) - self.setButtonEnablement(ButtonIdentifiers.loadInterstitialButtonIdentifier, enable: true) + self.setButtonEnablement(ButtonIdentifiers.loadInterstitialAdButtonIdentifier, enable: true) } @IBAction func loadInterstitialButtonTapped(_ sender: Any) { @@ -163,6 +159,67 @@ class DemoViewController: UIViewController, DemoViewControllerDelegate { } } + //MARK: Rewarded Video Methods + + func createRewardedAd() { + self.rewardedAd = LPMRewardedAd(adUnitId: rewardedAdAdUnitId) + rewardedAdDelegate = .init(delegate: self) + self.rewardedAd.setDelegate(rewardedAdDelegate) + + self.setButtonEnablement(ButtonIdentifiers.loadRewardedAdButtonIdentifier, enable: true) + } + + @IBAction func loadRewardedAdButtonTapped(_ sender: Any) { + guard let rewardedAd = self.rewardedAd else { + return + } + + // This will load a rewarded ad + logMethodName(string: "loadAd for rewarded") + rewardedAd.loadAd() + } + + @IBAction func showRewardedAdButtonTapped(_ sender: Any) { + // It is advised to make sure there is available ad before attempting to show an ad + if (self.rewardedAd != nil && self.rewardedAd.isAdReady()) { + // This will present the Rewarded ad. + + logMethodName(string: "showAd for rewarded") + self.rewardedAd.showAd(viewController: self, placementName: nil) + } else { + // load a new ad before calling show + } + } + + func setRewardInfo(_ reward: LPMReward?) { + // Setting the rewarded ad info, an object that contains the reward name and amount + self.reward = reward + } + + func showVideoRewardMessage() { + // Showing a graphical indication of the reward name and amount after the user closed the rewarded video ad + if (self.reward != nil) { + let rootViewController = UIApplication.shared.delegate?.window?!.rootViewController + let message = String(format: "You have been rewarded %d %@", reward.amount as Int, reward.name) + + let alert = UIAlertController( + title: "Video Reward", + message: message, + preferredStyle: .alert) + + let okAction = UIAlertAction( + title: "OK", + style: .default, + handler: { action in + }) + + alert.addAction(okAction) + rootViewController?.present(alert, animated: false) + + reward = nil + } + } + //MARK: Banner Methods @@ -183,7 +240,7 @@ class DemoViewController: UIViewController, DemoViewControllerDelegate { } // Create the banner ad view object with required & optional params - self.bannerAd = LPMBannerAdView(adUnitId: bannerAdUnitId) + self.bannerAd = LPMBannerAdView(adUnitId: bannerAdAdUnitId) self.bannerAd.setAdSize(bannerSize) // set the banner listener @@ -192,7 +249,7 @@ class DemoViewController: UIViewController, DemoViewControllerDelegate { addBannerToView() - self.setButtonEnablement(ButtonIdentifiers.loadBannerButtonIdentifier, enable: true) + self.setButtonEnablement(ButtonIdentifiers.loadBannerAdButtonIdentifier, enable: true) } func addBannerToView() { @@ -209,7 +266,7 @@ class DemoViewController: UIViewController, DemoViewControllerDelegate { } - @IBAction func loadBannerButtonTapped(_ sender: Any) { + @IBAction func loadBannerAdButtonTapped(_ sender: Any) { guard let bannerAd = self.bannerAd else { return @@ -219,50 +276,6 @@ class DemoViewController: UIViewController, DemoViewControllerDelegate { bannerAd.loadAd(with: self) } - - //MARK: Rewarded Video Methods - - @IBAction func showRewardedVideoButtonTapped(_ sender: Any) { - // It is advised to make sure there is available ad before attempting to show an ad - if IronSource.hasRewardedVideo() { - // This will present the Rewarded Video. - - logMethodName(string: "showRewardedVideo(with:)") - IronSource.showRewardedVideo(with: self) - } else { - // wait for the availability of rewarded video to change to true before calling show - } - } - - func setPlacementInfo(_ placementInfo: ISPlacementInfo?) { - // Setting the rewarded video placement info, an object that contains the placement's reward name and amount - self.rewardedVideoPlacementInfo = placementInfo - } - - func showVideoRewardMessage() { - // Showing a graphical indication of the reward name and amount after the user closed the rewarded video ad - if (self.rewardedVideoPlacementInfo != nil) { - let rootViewController = UIApplication.shared.delegate?.window?!.rootViewController - let message = String(format: "You have been rewarded %d %@", (rewardedVideoPlacementInfo.rewardAmount.intValue), rewardedVideoPlacementInfo.rewardName) - - let alert = UIAlertController( - title: "Video Reward", - message: message, - preferredStyle: .alert) - - let okAction = UIAlertAction( - title: "OK", - style: .default, - handler: { action in - }) - - alert.addAction(okAction) - rootViewController?.present(alert, animated: false) - - rewardedVideoPlacementInfo = nil - } - } - //MARK: Utility methods func setButtonEnablement(_ buttonIdentifier: ButtonIdentifiers, enable: Bool) { @@ -270,14 +283,16 @@ class DemoViewController: UIViewController, DemoViewControllerDelegate { var buttonToModify: UIButton? switch buttonIdentifier { - case .showRewardedVideoButtonIdentifier: - buttonToModify = self.showRewardedVideoButton - case .loadInterstitialButtonIdentifier: - buttonToModify = self.loadInterstitialButton - case .showInterstitialButtonIdentifier: - buttonToModify = self.showInterstitialButton - case .loadBannerButtonIdentifier: - buttonToModify = self.loadBannerButton + case .loadRewardedAdButtonIdentifier: + buttonToModify = self.loadRewardedAdButton + case .showRewardedAdButtonIdentifier: + buttonToModify = self.showRewardedAdButton + case .loadInterstitialAdButtonIdentifier: + buttonToModify = self.loadInterstitialAdButton + case .showInterstitialAdButtonIdentifier: + buttonToModify = self.showInterstitialAdButton + case .loadBannerAdButtonIdentifier: + buttonToModify = self.loadBannerAdButton } buttonToModify?.isEnabled = enable