forked from fyber-engineering/mopub-ios-mediation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTapjoyInterstitialCustomEvent.m
executable file
·208 lines (174 loc) · 8.47 KB
/
TapjoyInterstitialCustomEvent.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#import "TapjoyInterstitialCustomEvent.h"
#import "TapjoyAdapterConfiguration.h"
#import <Tapjoy/TJPlacement.h>
#import <Tapjoy/Tapjoy.h>
#if __has_include("MoPub.h")
#import "MPLogging.h"
#import "MoPub.h"
#endif
@interface TapjoyInterstitialCustomEvent () <TJPlacementDelegate>
@property (nonatomic, strong) TJPlacement *placement;
@property (nonatomic, assign) BOOL isConnecting;
@property (nonatomic, copy) NSString *placementName;
@end
@implementation TapjoyInterstitialCustomEvent
@dynamic delegate;
@dynamic localExtras;
@dynamic hasAdAvailable;
- (void)setupListeners {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(tjcConnectSuccess:)
name:TJC_CONNECT_SUCCESS
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(tjcConnectFail:)
name:TJC_CONNECT_FAILED
object:nil];
}
- (void)initializeWithCustomNetworkInfo:(NSDictionary *)info {
// Grab sdkKey and connect flags defined in MoPub dashboard
NSString *sdkKey = info[@"sdkKey"];
BOOL enableDebug = [info[@"debugEnabled"] boolValue];
if (sdkKey) {
MPLogInfo(@"Connecting to Tapjoy via MoPub dashboard settings");
NSMutableDictionary *connectOptions = [[NSMutableDictionary alloc] init];
[connectOptions setObject:@(enableDebug) forKey:TJC_OPTION_ENABLE_LOGGING];
[self setupListeners];
[Tapjoy connect:sdkKey options:connectOptions];
self.isConnecting = YES;
}
else {
NSError *error = [NSError errorWithCode:MOPUBErrorAdapterFailedToLoadAd localizedDescription:@"Tapjoy interstitial is initialized with empty 'sdkKey'. You must call Tapjoy connect before requesting content."];
MPLogAdEvent([MPLogEvent adLoadFailedForAdapter:NSStringFromClass(self.class) error:error], self.placementName);
[self.delegate fullscreenAdAdapter:self didFailToLoadAdWithError:error];
}
}
#pragma mark - MPFullscreenAdAdapter Override
- (BOOL)isRewardExpected {
return NO;
}
- (BOOL)hasAdAvailable {
return self.placement.isContentAvailable;
}
- (void)requestAdWithAdapterInfo:(NSDictionary *)info adMarkup:(NSString *)adMarkup {
// Grab placement name defined in MoPub dashboard as custom event data
self.placementName = info[@"name"];
// Cache network initialization info
[TapjoyAdapterConfiguration updateInitializationParameters:info];
// Adapter is making connect call on behalf of publisher, wait for success before requesting content.
if (self.isConnecting) {
return;
}
// Attempt to establish a connection to Tapjoy
if (![Tapjoy isConnected]) {
[self initializeWithCustomNetworkInfo:info];
}
// Live connection to Tapjoy already exists; request the ad
else {
MPLogInfo(@"Requesting Tapjoy interstitial");
[self requestPlacementContentWithAdMarkup:adMarkup];
}
}
- (void)requestPlacementContentWithAdMarkup:(NSString *)adMarkup {
if (self.placementName != nil) {
self.placement = [TJPlacement placementWithName:self.placementName mediationAgent:@"mopub" mediationId:nil delegate:self];
self.placement.adapterVersion = MP_SDK_VERSION;
// Advanced bidding response
if (adMarkup != nil) {
// Convert the JSON string into a dictionary.
NSData * jsonData = [adMarkup dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary * auctionData = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
if (auctionData != nil) {
[self.placement setAuctionData:auctionData];
}
}
MPLogAdEvent([MPLogEvent adLoadAttemptForAdapter:NSStringFromClass(self.class) dspCreativeId:nil dspName:nil], self.placementName);
[self.placement requestContent];
}
else {
NSError *error = [NSError errorWithCode:MOPUBErrorAdapterFailedToLoadAd localizedDescription:@"Invalid Tapjoy placement name specified"];
MPLogAdEvent([MPLogEvent adLoadFailedForAdapter:NSStringFromClass(self.class) error:error], nil);
[self.delegate fullscreenAdAdapter:self didFailToLoadAdWithError:error];
}
}
- (void)presentAdFromViewController:(UIViewController *)viewController {
MPLogAdEvent([MPLogEvent adShowAttemptForAdapter:NSStringFromClass(self.class)], self.placementName);
[self.placement showContentWithViewController:nil];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
_placement.delegate = nil;
}
- (BOOL)enableAutomaticImpressionAndClickTracking
{
return NO; // Disabled so adapters have control over the impression and click tracking behavior
}
#pragma mark - TJPlacementtDelegate
- (void)requestDidSucceed:(TJPlacement *)placement {
if (placement.isContentAvailable) {
MPLogAdEvent([MPLogEvent adLoadSuccessForAdapter:NSStringFromClass(self.class)], self.placementName);
[self.delegate fullscreenAdAdapterDidLoadAd:self];
}
else {
NSError *error = [NSError errorWithCode:MOPUBErrorAdapterFailedToLoadAd localizedDescription:@"No Tapjoy interstitials available"];
MPLogAdEvent([MPLogEvent adLoadFailedForAdapter:NSStringFromClass(self.class) error:error], self.placementName);
[self.delegate fullscreenAdAdapter:self didFailToLoadAdWithError:error];
}
}
- (void)requestDidFail:(TJPlacement *)placement error:(NSError *)error {
MPLogAdEvent([MPLogEvent adLoadFailedForAdapter:NSStringFromClass(self.class) error:error], self.placementName);
[self.delegate fullscreenAdAdapter:self didFailToLoadAdWithError:error];
}
- (void)contentDidAppear:(TJPlacement *)placement {
MPLogAdEvent([MPLogEvent adWillAppearForAdapter:NSStringFromClass(self.class)], self.placementName);
[self.delegate fullscreenAdAdapterAdWillPresent:self];
MPLogAdEvent([MPLogEvent adShowSuccessForAdapter:NSStringFromClass(self.class)], self.placementName);
MPLogAdEvent([MPLogEvent adDidAppearForAdapter:NSStringFromClass(self.class)], self.placementName);
[self.delegate fullscreenAdAdapterAdDidPresent:self];
[self.delegate fullscreenAdAdapterDidTrackImpression:self];
}
- (void)contentDidDisappear:(TJPlacement *)placement {
MPLogAdEvent([MPLogEvent adWillDisappearForAdapter:NSStringFromClass(self.class)], self.placementName);
[self.delegate fullscreenAdAdapterAdWillDismiss:self];
[self.delegate fullscreenAdAdapterAdWillDisappear:self];
MPLogAdEvent([MPLogEvent adDidDisappearForAdapter:NSStringFromClass(self.class)], self.placementName);
[self.delegate fullscreenAdAdapterAdDidDisappear:self];
[self.delegate fullscreenAdAdapterAdDidDismiss:self];
}
- (void)didClick:(TJPlacement*)placement
{
MPLogAdEvent([MPLogEvent adTappedForAdapter:NSStringFromClass(self.class)], self.placementName);
[self.delegate fullscreenAdAdapterDidReceiveTap:self];
[self.delegate fullscreenAdAdapterDidTrackClick:self];
MPLogAdEvent([MPLogEvent adWillLeaveApplicationForAdapter:NSStringFromClass(self.class)], self.placementName);
[self.delegate fullscreenAdAdapterWillLeaveApplication:self];
}
- (void)tjcConnectSuccess:(NSNotification*)notifyObj {
MPLogInfo(@"Tapjoy connect Succeeded");
self.isConnecting = NO;
[self fetchMoPubGDPRSettings];
[self requestPlacementContentWithAdMarkup:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)tjcConnectFail:(NSNotification*)notifyObj {
MPLogInfo(@"Tapjoy connect Failed");
self.isConnecting = NO;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
// Collect latest MoPub GDPR settings and pass them to Tapjoy
-(void)fetchMoPubGDPRSettings {
// If the GDPR applies setting is unknown, assume it has been skipped/unset
MPBool gdprApplies = [MoPub sharedInstance].isGDPRApplicable;
if (gdprApplies != MPBoolUnknown ) {
//Turn the MPBool into a proper bool
if(gdprApplies == MPBoolYes) {
[[Tapjoy getPrivacyPolicy] setSubjectToGDPR:YES];
NSString *consentString = [[MoPub sharedInstance] canCollectPersonalInfo] ? @"1" : @"0";
[[Tapjoy getPrivacyPolicy] setUserConsent: consentString];
} else {
[[Tapjoy getPrivacyPolicy] setSubjectToGDPR:NO];
[[Tapjoy getPrivacyPolicy] setUserConsent: @"-1"];
}
}
}
@end