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

Ad object-based API #1

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
4 changes: 0 additions & 4 deletions adapters/Unity/UnityAdapter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
7DA7826A1DDA681E000C088E /* GADMAdapterUnity.h in Headers */ = {isa = PBXBuildFile; fileRef = 7DA7825E1DDA681E000C088E /* GADMAdapterUnity.h */; };
7DA7826B1DDA681E000C088E /* GADMAdapterUnity.m in Sources */ = {isa = PBXBuildFile; fileRef = 7DA7825F1DDA681E000C088E /* GADMAdapterUnity.m */; };
7DA7826C1DDA681E000C088E /* GADMAdapterUnityConstants.h in Headers */ = {isa = PBXBuildFile; fileRef = 7DA782601DDA681E000C088E /* GADMAdapterUnityConstants.h */; };
7DA7826D1DDA681E000C088E /* GADMAdapterUnityProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = 7DA782611DDA681E000C088E /* GADMAdapterUnityProtocol.h */; };
7DA7826E1DDA681E000C088E /* GADMAdapterUnitySingleton.h in Headers */ = {isa = PBXBuildFile; fileRef = 7DA782621DDA681E000C088E /* GADMAdapterUnitySingleton.h */; };
7DA7826F1DDA681E000C088E /* GADMAdapterUnitySingleton.m in Sources */ = {isa = PBXBuildFile; fileRef = 7DA782631DDA681E000C088E /* GADMAdapterUnitySingleton.m */; };
7DA782701DDA681E000C088E /* GADMAdapterUnityWeakReference.h in Headers */ = {isa = PBXBuildFile; fileRef = 7DA782641DDA681E000C088E /* GADMAdapterUnityWeakReference.h */; };
Expand Down Expand Up @@ -70,7 +69,6 @@
7DA7825E1DDA681E000C088E /* GADMAdapterUnity.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GADMAdapterUnity.h; sourceTree = "<group>"; };
7DA7825F1DDA681E000C088E /* GADMAdapterUnity.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GADMAdapterUnity.m; sourceTree = "<group>"; };
7DA782601DDA681E000C088E /* GADMAdapterUnityConstants.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GADMAdapterUnityConstants.h; sourceTree = "<group>"; };
7DA782611DDA681E000C088E /* GADMAdapterUnityProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GADMAdapterUnityProtocol.h; sourceTree = "<group>"; };
7DA782621DDA681E000C088E /* GADMAdapterUnitySingleton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GADMAdapterUnitySingleton.h; sourceTree = "<group>"; };
7DA782631DDA681E000C088E /* GADMAdapterUnitySingleton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GADMAdapterUnitySingleton.m; sourceTree = "<group>"; };
7DA782641DDA681E000C088E /* GADMAdapterUnityWeakReference.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GADMAdapterUnityWeakReference.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -125,7 +123,6 @@
7DA7825E1DDA681E000C088E /* GADMAdapterUnity.h */,
7DA7825F1DDA681E000C088E /* GADMAdapterUnity.m */,
7DA782601DDA681E000C088E /* GADMAdapterUnityConstants.h */,
7DA782611DDA681E000C088E /* GADMAdapterUnityProtocol.h */,
7DA782621DDA681E000C088E /* GADMAdapterUnitySingleton.h */,
7DA782631DDA681E000C088E /* GADMAdapterUnitySingleton.m */,
7DA782641DDA681E000C088E /* GADMAdapterUnityWeakReference.h */,
Expand Down Expand Up @@ -177,7 +174,6 @@
7DA782681DDA681E000C088E /* UnityAdapter.h in Headers */,
7DA782701DDA681E000C088E /* GADMAdapterUnityWeakReference.h in Headers */,
7DA7826E1DDA681E000C088E /* GADMAdapterUnitySingleton.h in Headers */,
7DA7826D1DDA681E000C088E /* GADMAdapterUnityProtocol.h in Headers */,
7DA7826C1DDA681E000C088E /* GADMAdapterUnityConstants.h in Headers */,
7DA782721DDA681E000C088E /* GADUnityError.h in Headers */,
7DA7826A1DDA681E000C088E /* GADMAdapterUnity.h in Headers */,
Expand Down
5 changes: 1 addition & 4 deletions adapters/Unity/UnityAdapter/GADMAdapterUnity.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@
@import GoogleMobileAds;
@import UnityAds;

#import "GADMAdapterUnityProtocol.h"

/// Adapter for communicating with the Unity Ads Network to fetch reward-based video ads and
/// interstitial ads.
@interface GADMAdapterUnity : NSObject <GADMRewardBasedVideoAdNetworkAdapter, GADMAdNetworkAdapter,
GADMAdapterUnityDataProvider, UnityAdsExtendedDelegate>
@interface GADMAdapterUnity : NSObject <GADMRewardBasedVideoAdNetworkAdapter, GADMAdNetworkAdapter>

@end
251 changes: 132 additions & 119 deletions adapters/Unity/UnityAdapter/GADMAdapterUnity.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@
#import "GADMAdapterUnitySingleton.h"
#import "GADUnityError.h"

@interface GADMAdapterUnity () {
@interface GADMAdapterUnity () <UADSInterstitialAdDelegate, UADSRewardedVideoAdDelegate, UADSBannerAdDelegate> {
/// Connector from Google Mobile Ads SDK to receive ad configurations.
__weak id<GADMRewardBasedVideoAdNetworkConnector> _rewardBasedVideoAdConnector;

/// Connector from Google Mobile Ads SDK to receive ad configurations.
__weak id<GADMAdNetworkConnector> _interstitialConnector;
__weak id<GADMAdNetworkConnector> _networkConnector;

/// Placement ID of Unity Ads network.
NSString *_placementID;

/// YES if the adapter is loading.
BOOL _isLoading;
UADSInterstitialAd* _interstitialAd;
UADSBannerAd* _bannerAd;
UADSRewardedVideoAd* _rewardedVideoAd;
}

@end
Expand Down Expand Up @@ -70,9 +71,7 @@ - (void)setUp {
[strongConnector adapter:self didFailToSetUpRewardBasedVideoAdWithError:error];
return;
}
BOOL isConfigured =
[[GADMAdapterUnitySingleton sharedInstance] configureRewardBasedVideoAdWithGameID:gameID
delegate:self];
BOOL isConfigured = [[GADMAdapterUnitySingleton sharedInstance] configureWithGameID:gameID];
if (isConfigured) {
[strongConnector adapterDidSetUpRewardBasedVideoAd:self];
} else {
Expand All @@ -85,23 +84,15 @@ - (void)setUp {
}

- (void)requestRewardBasedVideoAd {
_placementID =
[[[_rewardBasedVideoAdConnector credentials] objectForKey:GADMAdapterUnityPlacementID] copy];
_isLoading = YES;
[[GADMAdapterUnitySingleton sharedInstance] requestRewardBasedVideoAdWithDelegate:self];
if (_rewardedVideoAd == nil) {
_rewardedVideoAd = [[UADSRewardedVideoAd alloc] initWithPlacementId:_placementID];
_rewardedVideoAd.delegate = self;
[_rewardedVideoAd load];
}
}

- (void)presentRewardBasedVideoAdWithRootViewController:(UIViewController *)viewController {
// We will send adapterDidOpenRewardBasedVideoAd callback before presenting the unity ad because
// the ad has already loaded.
[_rewardBasedVideoAdConnector adapterDidOpenRewardBasedVideoAd:self];
[[GADMAdapterUnitySingleton sharedInstance]
presentRewardBasedVideoAdForViewController:viewController
delegate:self];
}

- (void)stopBeingDelegate {
[[GADMAdapterUnitySingleton sharedInstance] stopTrackingDelegate:self];
[_rewardedVideoAd showFromViewController:viewController];
}

#pragma mark Interstitial Methods
Expand All @@ -113,13 +104,13 @@ - (instancetype)initWithGADMAdNetworkConnector:(id<GADMAdNetworkConnector>)conne

self = [super init];
if (self) {
_interstitialConnector = connector;
_networkConnector = connector;
}
return self;
}

- (void)getInterstitial {
id<GADMAdNetworkConnector> strongConnector = _interstitialConnector;
id<GADMAdNetworkConnector> strongConnector = _networkConnector;
NSString *gameID =
[[[strongConnector credentials] objectForKey:GADMAdapterUnityGameID] copy];
_placementID =
Expand All @@ -129,130 +120,152 @@ - (void)getInterstitial {
[strongConnector adapter:self didFailAd:error];
return;
}
_isLoading = YES;
[[GADMAdapterUnitySingleton sharedInstance] configureInterstitialAdWithGameID:gameID
delegate:self];
BOOL isConfigured = [[GADMAdapterUnitySingleton sharedInstance] configureWithGameID:gameID];
if (!isConfigured) {
NSString *description =
[[NSString alloc] initWithFormat:@"%@ is not supported for this device.",
NSStringFromClass([UnityAds class])];
NSError *error = GADUnityErrorWithDescription(description);
[strongConnector adapter:self didFailAd:error];
} else {
if (!_interstitialAd) {
_interstitialAd = [[UADSInterstitialAd alloc] initWithPlacementId:_placementID];
_interstitialAd.delegate = self;
[_interstitialAd load];
}
}
}

- (void)presentInterstitialFromRootViewController:(UIViewController *)rootViewController {
// We will send adapterWillPresentInterstitial callback before presenting unity ad because the ad
// has already loaded.
[_interstitialConnector adapterWillPresentInterstitial:self];
[[GADMAdapterUnitySingleton sharedInstance]
presentInterstitialAdForViewController:rootViewController
delegate:self];
[_networkConnector adapterWillPresentInterstitial:self];
[_interstitialAd showFromViewController:rootViewController];
}

- (void)stopBeingDelegate {
}

#pragma mark Banner Methods

- (void)getBannerWithSize:(GADAdSize)adSize {
// Unity Ads doesn't support banner ads.
id<GADMAdNetworkConnector> strongConnector = _interstitialConnector;
NSError *error = GADUnityErrorWithDescription(@"Unity Ads doesn't support banner ads.");
[strongConnector adapter:self didFailAd:error];
id<GADMAdNetworkConnector> strongNetworkConnector = _networkConnector;
NSString *gameID =
[[[strongNetworkConnector credentials] objectForKey:GADMAdapterUnityGameID] copy];
_placementID =
[[[strongNetworkConnector credentials] objectForKey:GADMAdapterUnityPlacementID] copy];
if (!gameID || !_placementID) {
NSError *error = GADUnityErrorWithDescription(@"Game ID and Placement ID cannot be nil.");
[strongNetworkConnector adapter:self didFailAd:error];
return;
}
BOOL isConfigured = [[GADMAdapterUnitySingleton sharedInstance] configureWithGameID:gameID];
if (!isConfigured) {
NSString *description =
[[NSString alloc] initWithFormat:@"%@ is not supported for this device.",
NSStringFromClass([UnityAds class])];
NSError *error = GADUnityErrorWithDescription(description);
[strongNetworkConnector adapter:self didFailAd:error];
} else {
if (!_bannerAd) {
_bannerAd = [[UADSBannerAd alloc] initWithPlacementId:_placementID];
_bannerAd.delegate = self;
[_bannerAd load];
}
}
}

- (BOOL)isBannerAnimationOK:(GADMBannerAnimationType)animType {
return YES;
}

#pragma mark GADMAdapterUnityDataProvider Methods
#pragma mark - UADSInterstitialAdDelegate

- (NSString *)getPlacementID {
return _placementID;
-(void)interstitialAdDidLoad:(UADSInterstitialAd *)interstitialAd {
[_networkConnector adapterDidReceiveInterstitial:self];
}
-(void)interstitialAdDidFailToLoad:(UADSInterstitialAd *)interstitialAd error:(NSError *)error {
[_networkConnector adapter:self didFailAd:GADUnityErrorWithDescription([error description])];
}
-(void)interstitialAdDidOpen:(UADSInterstitialAd *)interstitialAd {
}

#pragma mark - Unity Delegate Methods
-(void)interstitialAdDidClick:(UADSInterstitialAd *)interstitialAd {
[_networkConnector adapterDidGetAdClick:self];
}
-(void)interstitialAdDidLeaveApplication:(UADSInterstitialAd *)interstitialAd {
[_networkConnector adapterWillLeaveApplication:self];
}
-(void)interstitialAdDidInvalidate:(UADSInterstitialAd *)interstitialAd {
}

- (void)unityAdsPlacementStateChanged:(NSString *)placementId
oldState:(UnityAdsPlacementState)oldState
newState:(UnityAdsPlacementState)newState {
// This callback is not forwarded to the adapter by the GADMAdapterUnitySingleton and the adapter
// should use the unityAdsReady: and unityAdsDidError: callbacks to forward Unity Ads SDK state to
// Google Mobile Ads SDK.
-(void)interstitialAdDidClose:(UADSInterstitialAd *)interstitialAd finishState:(UnityAdsFinishState)finishState {
[_networkConnector adapterWillDismissInterstitial:self];
_interstitialAd = nil;
[_networkConnector adapterDidDismissInterstitial:self];
}

- (void)unityAdsDidFinish:(NSString *)placementID withFinishState:(UnityAdsFinishState)state {
id<GADMAdNetworkConnector> strongInterstitialConnector = _interstitialConnector;
id<GADMRewardBasedVideoAdNetworkConnector> strongRewardedConnector = _rewardBasedVideoAdConnector;
if (strongInterstitialConnector) {
[strongInterstitialConnector adapterWillDismissInterstitial:self];
[strongInterstitialConnector adapterDidDismissInterstitial:self];
} else if (strongRewardedConnector) {
if (state == kUnityAdsFinishStateCompleted) {
[strongRewardedConnector adapterDidCompletePlayingRewardBasedVideoAd:self];
// Unity Ads doesn't provide a way to set the reward on their front-end. Default to a reward
// amount of 1. Publishers using this adapter should override the reward on the AdMob
// front-end.
GADAdReward *reward =
[[GADAdReward alloc] initWithRewardType:@"" rewardAmount:[NSDecimalNumber one]];
[strongRewardedConnector adapter:self didRewardUserWithReward:reward];
}
[strongRewardedConnector adapterDidCloseRewardBasedVideoAd:self];
}
#pragma mark - UADSRewardedVideoAdDelegate

-(void)rewardedVideoAdDidLoad:(UADSRewardedVideoAd *)rewardedVideoAd {
[_rewardBasedVideoAdConnector adapterDidReceiveRewardBasedVideoAd:self];
}
-(void)rewardedVideoAdDidFailToLoad:(UADSRewardedVideoAd *)rewardedVideoAd exception:(NSException *)exception {
[_rewardBasedVideoAdConnector adapter:self didFailToLoadRewardBasedVideoAdwithError:GADUnityErrorWithDescription([exception description])];
}
-(void)rewardedVideoAdDidOpen:(UADSRewardedVideoAd *)rewardedVideoAd {
[_rewardBasedVideoAdConnector adapterDidOpenRewardBasedVideoAd:self];
}
-(void)rewardedVideoAdDidStart:(UADSRewardedVideoAd *)rewardedVideoAd {
[_rewardBasedVideoAdConnector adapterDidStartPlayingRewardBasedVideoAd:self];
}

- (void)unityAdsDidStart:(NSString *)placementID {
id<GADMRewardBasedVideoAdNetworkConnector> strongRewardedConnector = _rewardBasedVideoAdConnector;
if (strongRewardedConnector) {
[strongRewardedConnector adapterDidStartPlayingRewardBasedVideoAd:self];
}
-(void)rewardedVideoAdDidClick:(UADSRewardedVideoAd *)rewardedVideoAd {
[_rewardBasedVideoAdConnector adapterDidGetAdClick:self];
}
-(void)rewardedVideoAdDidLeaveApplication:(UADSRewardedVideoAd *)rewardedVideoAd {
[_rewardBasedVideoAdConnector adapterWillLeaveApplication:self];
}
-(void)rewardedVideoAdDidReward:(UADSRewardedVideoAd *)rewardedVideoAd {
GADAdReward *reward = [[GADAdReward alloc] initWithRewardType:@"" rewardAmount:[NSDecimalNumber one]];
[_rewardBasedVideoAdConnector adapter:self didRewardUserWithReward:reward];
}
-(void)rewardedVideoAdDidClose:(UADSRewardedVideoAd *)rewardedVideoAd finishState:(UnityAdsFinishState)finishState {
[_rewardBasedVideoAdConnector adapterDidCloseRewardBasedVideoAd:self];
_rewardedVideoAd = nil;
}
-(void)rewardedVideoAdDidInvalidate:(UADSRewardedVideoAd *)rewardedVideoAd {

- (void)unityAdsReady:(NSString *)placementID {
id<GADMAdNetworkConnector> strongInterstitialConnector = _interstitialConnector;
if (!_isLoading) {
return;
}
}
-(void)rewardedVideoAdDidFinish:(UADSRewardedVideoAd *)rewardedVideoAd {
[_rewardBasedVideoAdConnector adapterDidCompletePlayingRewardBasedVideoAd:self];
}

if (strongInterstitialConnector) {
[strongInterstitialConnector adapterDidReceiveInterstitial:self];
} else {
[_rewardBasedVideoAdConnector adapterDidReceiveRewardBasedVideoAd:self];
}
_isLoading = NO;
}

- (void)unityAdsDidClick:(NSString *)placementID {
id<GADMAdNetworkConnector> strongInterstitialConnector = _interstitialConnector;
id<GADMRewardBasedVideoAdNetworkConnector> strongRewardedConnector = _rewardBasedVideoAdConnector;
// The Unity Ads SDK doesn't provide an event for leaving the application, so the adapter assumes
// that a click event indicates the user is leaving the application for a browser or deeplink, and
// notifies the Google Mobile Ads SDK accordingly.
if (strongInterstitialConnector) {
[strongInterstitialConnector adapterDidGetAdClick:self];
[strongInterstitialConnector adapterWillLeaveApplication:self];
} else {
[strongRewardedConnector adapterDidGetAdClick:self];
[strongRewardedConnector adapterWillLeaveApplication:self];
}
#pragma mark - UADSBannerAdDelegate

-(void)bannerAdDidLoad:(UADSBannerAd *)bannerAd {
[_networkConnector adapter:self didReceiveAdView:[bannerAd getView]];
}

- (void)unityAdsDidError:(UnityAdsError)error withMessage:(NSString *)message {
id<GADMAdNetworkConnector> strongInterstitialConnector = _interstitialConnector;
id<GADMRewardBasedVideoAdNetworkConnector> strongRewardedConnector = _rewardBasedVideoAdConnector;
if (!_isLoading) {
// Unity Ads show error will only happen after the ad has been loaded. So, we will send
// dismiss/close callbacks.
if (error == kUnityAdsErrorShowError) {
if (strongInterstitialConnector) {
[strongInterstitialConnector adapterWillDismissInterstitial:self];
[strongInterstitialConnector adapterDidDismissInterstitial:self];
} else {
[strongRewardedConnector adapterDidCloseRewardBasedVideoAd:self];
}
}
return;
}
-(void)bannerAdDidFailToLoad:(UADSBannerAd *)bannerAd error:(NSError *)error {
[_networkConnector adapter:self didFailAd:GADUnityErrorWithDescription([error description])];
}

NSError *errorWithDescription = GADUnityErrorWithDescription(message);
if (strongInterstitialConnector) {
[strongInterstitialConnector adapter:self didFailAd:errorWithDescription];
} else if (strongRewardedConnector) {
[strongRewardedConnector adapter:self
didFailToLoadRewardBasedVideoAdwithError:errorWithDescription];
}
_isLoading = NO;
-(void)bannerAdDidOpen:(UADSBannerAd *)bannerAd {
}

-(void)bannerAdDidClose:(UADSBannerAd *)bannerAd {
}

-(void)bannerAdDidClick:(UADSBannerAd *)bannerAd {
[_networkConnector adapterDidGetAdClick:self];
}
-(void)bannerAdDidLeaveApplication:(UADSBannerAd *)bannerAd {
[_networkConnector adapterWillLeaveApplication:self];
}

-(void)bannerAdDidInvalidate:(UADSBannerAd *)bannerAd {
}


@end


2 changes: 1 addition & 1 deletion adapters/Unity/UnityAdapter/GADMAdapterUnityConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static NSString *const GADMAdapterUnityGameID = @"gameId";
static NSString *const GADMAdapterUnityPlacementID = @"zoneId";

/// Ad mediation network adapter version.
static NSString *const GADMAdapterUnityVersion = @"3.0.0.1";
static NSString *const GADMAdapterUnityVersion = @"3.0.1.0";

/// Ad mediation network name.
static NSString *const GADMAdapterUnityMediationNetworkName = @"AdMob";
Loading