Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewarded Ad Unit Enhancements #1058

Merged
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions EventHandlers/EventHandlers.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
344A7F0E250B749600743A93 /* GAMRequestWrapperTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 344A7F0D250B749600743A93 /* GAMRequestWrapperTest.swift */; };
344A7F14250B823F00743A93 /* GAMInterstitialAdWrapperTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 344A7F13250B823F00743A93 /* GAMInterstitialAdWrapperTest.swift */; };
344A7F1A250B8E5400743A93 /* GADRewardedAdWrapperTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 344A7F19250B8E5400743A93 /* GADRewardedAdWrapperTest.swift */; };
534C61682CB861930026119A /* PrebidAdMobRewardedAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 534C61672CB861930026119A /* PrebidAdMobRewardedAdapter.swift */; };
53632B24290BC57A00139EF3 /* AdMobGMATests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53632B23290BC57A00139EF3 /* AdMobGMATests.swift */; };
53632B2C290BE17300139EF3 /* GMATests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53632B2B290BE17300139EF3 /* GMATests.swift */; };
53F2A57B2CB94FBE009B74CA /* GADReward+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53F2A57A2CB94FB3009B74CA /* GADReward+Extensions.swift */; };
5B36172626370A8700AFA1C2 /* GAMUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B36172526370A8700AFA1C2 /* GAMUtils.swift */; };
5B3617FC2638131A00AFA1C2 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B3617FB2638131A00AFA1C2 /* Constants.swift */; };
5B36182326384C7D00AFA1C2 /* GAMRewardedEventHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B36182226384C7D00AFA1C2 /* GAMRewardedEventHandler.swift */; };
Expand Down Expand Up @@ -112,8 +114,10 @@
34B64D5F24F7E41600FDD9F4 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
39183ECBC4E402BC48B3691D /* Pods_PrebidMobileGAMEventHandlersTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_PrebidMobileGAMEventHandlersTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
42CE6323AC67980202BFDE74 /* Pods-PrebidMobileAdMobAdapters.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PrebidMobileAdMobAdapters.release.xcconfig"; path = "Target Support Files/Pods-PrebidMobileAdMobAdapters/Pods-PrebidMobileAdMobAdapters.release.xcconfig"; sourceTree = "<group>"; };
534C61672CB861930026119A /* PrebidAdMobRewardedAdapter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrebidAdMobRewardedAdapter.swift; sourceTree = "<group>"; };
53632B23290BC57A00139EF3 /* AdMobGMATests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdMobGMATests.swift; sourceTree = "<group>"; };
53632B2B290BE17300139EF3 /* GMATests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GMATests.swift; sourceTree = "<group>"; };
53F2A57A2CB94FB3009B74CA /* GADReward+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "GADReward+Extensions.swift"; sourceTree = "<group>"; };
58B193FE14C1F3D7E795F7AC /* Pods-PrebidMobileMAXAdapters.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PrebidMobileMAXAdapters.release.xcconfig"; path = "Target Support Files/Pods-PrebidMobileMAXAdapters/Pods-PrebidMobileMAXAdapters.release.xcconfig"; sourceTree = "<group>"; };
5B36172526370A8700AFA1C2 /* GAMUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GAMUtils.swift; sourceTree = "<group>"; };
5B3617FB2638131A00AFA1C2 /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -336,14 +340,15 @@
34B64D6D24F7E54700FDD9F4 /* Sources */ = {
isa = PBXGroup;
children = (
34C7C9E8250901410021C6D0 /* Boxes */,
53F2A5792CB94FA8009B74CA /* Utils */,
5B69677F2642A56A0042372E /* PrebidMobileGAMEventHandlers.h */,
5B3617FB2638131A00AFA1C2 /* Constants.swift */,
5BF8E76D263BF4DF00341EE4 /* GAMBannerEventHandler.swift */,
5B69676F2642993B0042372E /* GAMEventHandlerError.swift */,
5BF8E6B6263AC32800341EE4 /* GAMInterstitialEventHandler.swift */,
5B36182226384C7D00AFA1C2 /* GAMRewardedEventHandler.swift */,
5B36172526370A8700AFA1C2 /* GAMUtils.swift */,
34C7C9E8250901410021C6D0 /* Boxes */,
);
path = Sources;
sourceTree = "<group>";
Expand All @@ -361,6 +366,14 @@
path = Boxes;
sourceTree = "<group>";
};
53F2A5792CB94FA8009B74CA /* Utils */ = {
isa = PBXGroup;
children = (
53F2A57A2CB94FB3009B74CA /* GADReward+Extensions.swift */,
);
path = Utils;
sourceTree = "<group>";
};
927ADB28280583AF006EB8D5 /* Sources */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -390,12 +403,13 @@
92C7A868279AAEF1000D31CB /* AdMobMediationNativeUtils.swift */,
92C474EC2795B57E00C26E27 /* AdMobMediationRewardedUtils.swift */,
92A2380C27731B48000157AA /* AdMobUtils.swift */,
92C474F42795E1A600C26E27 /* PrebidAdMobMediationBaseAdapter.swift */,
92E924FA2769ED96002B57F3 /* PrebidAdMobBannerAdapter.swift */,
926CBF1B278C837800E07BF2 /* PrebidAdMobInterstitialAdapter.swift */,
92C474F42795E1A600C26E27 /* PrebidAdMobMediationBaseAdapter.swift */,
92EB6DE42799FB4700470DEC /* PrebidAdMobNativeAdapter.swift */,
92DDA43B278EE8EC0035F03E /* PrebidAdMobRewardedVideoAdapter.swift */,
9285B46527AAC5B1009BD3D3 /* PrebidAdMobVideoInterstitialAdapter.swift */,
534C61672CB861930026119A /* PrebidAdMobRewardedAdapter.swift */,
92DDA43B278EE8EC0035F03E /* PrebidAdMobRewardedVideoAdapter.swift */,
92EB6DE42799FB4700470DEC /* PrebidAdMobNativeAdapter.swift */,
);
path = Sources;
sourceTree = "<group>";
Expand Down Expand Up @@ -951,6 +965,7 @@
5BF8E781263C363800341EE4 /* GAMRequestWrapper.swift in Sources */,
5BF8E789263C3B4000341EE4 /* GADNativeAdWrapper.swift in Sources */,
5B36172626370A8700AFA1C2 /* GAMUtils.swift in Sources */,
53F2A57B2CB94FBE009B74CA /* GADReward+Extensions.swift in Sources */,
5B6967702642993B0042372E /* GAMEventHandlerError.swift in Sources */,
5BF8E779263C291300341EE4 /* GAMBannerViewWrapper.swift in Sources */,
);
Expand Down Expand Up @@ -990,6 +1005,7 @@
9267B400276FCF9300F1ADF1 /* AdMobConstants.swift in Sources */,
92C474F52795E1A600C26E27 /* PrebidAdMobMediationBaseAdapter.swift in Sources */,
92E924FD276A0FD8002B57F3 /* AdMobMediationBannerUtils.swift in Sources */,
534C61682CB861930026119A /* PrebidAdMobRewardedAdapter.swift in Sources */,
9285B46627AAC5B1009BD3D3 /* PrebidAdMobVideoInterstitialAdapter.swift in Sources */,
92C7A869279AAEF2000D31CB /* AdMobMediationNativeUtils.swift in Sources */,
92E924FF276A193E002B57F3 /* AdMobAdaptersError.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ public class PrebidAdMobMediationBaseAdapter: NSObject, GADMediationAdapter {
super.init()
}

public static func setUpWith(_ configuration: GADMediationServerConfiguration, completionHandler: @escaping GADMediationAdapterSetUpCompletionBlock) {
public static func setUpWith(
_ configuration: GADMediationServerConfiguration,
completionHandler: @escaping GADMediationAdapterSetUpCompletionBlock
) {
// TODO: Add Prebid SDK initialization logic
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*   Copyright 2018-2021 Prebid.org, Inc.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

import PrebidMobile
import GoogleMobileAds

@objc(PrebidAdMobRewardedAdapter)
public class PrebidAdMobRewardedAdapter:
PrebidAdMobMediationBaseAdapter,
GADMediationRewardedAd,
InterstitialControllerLoadingDelegate,
InterstitialControllerInteractionDelegate {

// MARK: - Private Properties

var interstitialController: InterstitialController?
weak var rootViewController: UIViewController?
var adAvailable = false

weak var delegate: GADMediationRewardedAdEventDelegate?
var completionHandler: GADMediationRewardedLoadCompletionHandler?

// MARK: - GADMediationAdapter

public func loadRewardedAd(for adConfiguration: GADMediationRewardedAdConfiguration,
completionHandler: @escaping GADMediationRewardedLoadCompletionHandler) {
self.completionHandler = completionHandler

switch createInterstitialController(with: adConfiguration) {
case .success(let controller):
self.interstitialController = controller
interstitialController?.loadAd()
case .failure(let error):
delegate = completionHandler(nil, error)
}
}

// MARK: - Helpers

func createInterstitialController(
with adConfiguration: GADMediationRewardedAdConfiguration
) -> Result<InterstitialController, Error> {

guard let serverParameter = adConfiguration.credentials.settings["parameter"] as? String else {
return .failure(AdMobAdaptersError.noServerParameter)
}

guard let eventExtras = adConfiguration.extras as? GADCustomEventExtras,
let eventExtrasDictionary = eventExtras.extras(forLabel: AdMobConstants.PrebidAdMobEventExtrasLabel),
!eventExtrasDictionary.isEmpty else {
return .failure(AdMobAdaptersError.emptyCustomEventExtras)
}

guard let targetingInfo = eventExtrasDictionary[PBMMediationTargetingInfoKey] as? [String: String] else {
return .failure(AdMobAdaptersError.noTargetingInfoInEventExtras)
}

guard MediationUtils.isServerParameterInTargetingInfoDict(serverParameter, targetingInfo) else {
return .failure(AdMobAdaptersError.wrongServerParameter)
}

guard let bid = eventExtrasDictionary[PBMMediationAdUnitBidKey] as? Bid else {
return .failure(AdMobAdaptersError.noBidInEventExtras)
}

guard let configId = eventExtrasDictionary[PBMMediationConfigIdKey] as? String else {
return .failure(AdMobAdaptersError.noConfigIDInEventExtras)
}

let interstitialController = InterstitialController(bid: bid, configId: configId)
interstitialController.loadingDelegate = self
interstitialController.interactionDelegate = self
interstitialController.isRewarded = true

if let videoAdConfig = eventExtrasDictionary[PBMMediationVideoAdConfiguration] as? VideoControlsConfiguration {
interstitialController.videoControlsConfig = videoAdConfig
}

if let videoParameters = eventExtrasDictionary[PBMMediationVideoParameters] as? VideoParameters {
interstitialController.videoParameters = videoParameters
}

return .success(interstitialController)
}

// MARK: - GADMediationRewardedAd

public func present(from viewController: UIViewController) {
if adAvailable {
rootViewController = viewController
interstitialController?.show()
} else {
let error = AdMobAdaptersError.noAd
delegate?.didFailToPresentWithError(error)

if let handler = completionHandler {
delegate = handler(nil, error)
}
}
}

// MARK: - InterstitialControllerLoadingDelegate

public func interstitialControllerDidLoadAd(_ interstitialController: InterstitialController) {
adAvailable = true

if let handler = completionHandler {
delegate = handler(self, nil)
}
}

public func interstitialController(_ interstitialController: InterstitialController, didFailWithError error: Error) {
adAvailable = false
}

// MARK: - InterstitialControllerInteractionDelegate

public func trackImpression(forInterstitialController: InterstitialController) {
delegate?.reportImpression()
}

public func trackUserReward(_ interstitialController: InterstitialController, _ reward: PrebidReward) {
delegate?.didRewardUser()
}

public func interstitialControllerDidClickAd(_ interstitialController: InterstitialController) {
delegate?.reportClick()
}

public func interstitialControllerDidCloseAd(_ interstitialController: InterstitialController) {
adAvailable = false
delegate?.willDismissFullScreenView()
delegate?.didDismissFullScreenView()
}

public func interstitialControllerDidDisplay(_ interstitialController: InterstitialController) {
delegate?.willPresentFullScreenView()
delegate?.didStartVideo()
delegate?.didEndVideo()
}

public func interstitialControllerDidComplete(_ interstitialController: InterstitialController) {
adAvailable = false
rootViewController = nil

delegate?.didRewardUser()
}

public func viewControllerForModalPresentation(fromInterstitialController: InterstitialController) -> UIViewController? {
rootViewController
}

public func interstitialControllerDidLeaveApp(_ interstitialController: InterstitialController) {}
}
Loading