forked from fyber-engineering/mopub-ios-mediation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTapjoyRewardedVideoCustomEvent.m
executable file
·267 lines (222 loc) · 10.8 KB
/
TapjoyRewardedVideoCustomEvent.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#import "TapjoyRewardedVideoCustomEvent.h"
#import "TapjoyAdapterConfiguration.h"
#import <Tapjoy/Tapjoy.h>
#import <Tapjoy/TJPlacement.h>
#if __has_include("MoPub.h")
#import "MPLogging.h"
#import "MPReward.h"
#import "MoPub.h"
#endif
#import "TapjoyGlobalMediationSettings.h"
@interface TapjoyRewardedVideoCustomEvent () <TJPlacementDelegate, TJPlacementVideoDelegate>
@property (nonatomic, strong) TJPlacement *placement;
@property (nonatomic, assign) BOOL isConnecting;
@property (nonatomic, copy) NSString *placementName;
@end
@implementation TapjoyRewardedVideoCustomEvent
@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 {
//Instantiate Mediation Settings
TapjoyGlobalMediationSettings *medSettings = [[MoPub sharedInstance] globalMediationSettingsForClass:[TapjoyGlobalMediationSettings class]];
// Grab sdkKey and connect flags defined in MoPub dashboard
NSString *sdkKey = info[@"sdkKey"];
BOOL enableDebug = [info[@"debugEnabled"] boolValue];
if (medSettings.sdkKey) {
MPLogInfo(@"Connecting to Tapjoy via MoPub mediation settings");
[self setupListeners];
[Tapjoy connect:medSettings.sdkKey options:medSettings.connectFlags];
self.isConnecting = YES;
}
else 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 rewarded video 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];
}
}
- (void)initializeSdkWithParameters:(NSDictionary *)parameters {
// Attempt to establish a connection to Tapjoy
if (![Tapjoy isConnected]) {
[self initializeWithCustomNetworkInfo:parameters];
}
}
#pragma mark - MPFullscreenAdAdapter Override
- (BOOL)isRewardExpected {
return YES;
}
- (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 rewarded video");
[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;
self.placement.videoDelegate = self;
// 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], self.placementName);
[self.delegate fullscreenAdAdapter:self didFailToLoadAdWithError:error];
}
}
- (void)presentAdFromViewController:(UIViewController *)viewController {
if ([self hasAdAvailable]) {
MPLogAdEvent([MPLogEvent adShowAttemptForAdapter:NSStringFromClass(self.class)], self.placementName);
[self.placement showContentWithViewController:nil];
}
else {
NSError *error = [NSError errorWithDomain:MoPubRewardedAdsSDKDomain code:MPRewardedAdErrorNoAdsAvailable userInfo:nil];
MPLogAdEvent([MPLogEvent adShowFailedForAdapter:NSStringFromClass(self.class) error:error], self.placementName);
[self.delegate fullscreenAdAdapter:self didFailToShowAdWithError:error];
}
}
- (void)handleDidInvalidateAd {
self.placement.delegate = nil;
}
- (void)handleDidPlayAd {
// If we no longer have an ad available, report back up to the application that this ad expired.
// We receive this message only when this ad has reported an ad has loaded and another ad unit
// has played a video for the same ad network.
if (![self hasAdAvailable]) {
[self.delegate fullscreenAdAdapterDidExpire:self];
}
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
_placement.delegate = nil;
}
- (BOOL)enableAutomaticImpressionAndClickTracking
{
return NO;
}
#pragma mark - TJPlacementDelegate methods
- (void)requestDidSucceed:(TJPlacement *)placement {
if (!placement.isContentAvailable) {
NSError *error = [NSError errorWithDomain:MoPubRewardedAdsSDKDomain code:MPRewardedAdErrorNoAdsAvailable userInfo:nil];
MPLogAdEvent([MPLogEvent adLoadFailedForAdapter:NSStringFromClass(self.class) error:error], self.placementName);
[self.delegate fullscreenAdAdapter:self didFailToLoadAdWithError:error];
}
}
- (void)contentIsReady:(TJPlacement *)placement {
MPLogInfo(@"Tapjoy rewarded video content is ready");
MPLogAdEvent([MPLogEvent adLoadSuccessForAdapter:NSStringFromClass(self.class)], self.placementName);
[self.delegate fullscreenAdAdapterDidLoadAd:self];
}
- (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 {
[Tapjoy setVideoAdDelegate:nil];
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];
}
#pragma mark Tapjoy Video
- (void)videoDidStart:(TJPlacement *)placement {
MPLogAdEvent([MPLogEvent adWillAppearForAdapter:NSStringFromClass(self.class)], self.placementName);
}
- (void)videoDidComplete:(TJPlacement*)placement {
MPReward *reward = [[MPReward alloc] initWithCurrencyAmount:@(kMPRewardCurrencyAmountUnspecified)];
[self.delegate fullscreenAdAdapter:self willRewardUser:reward];
}
- (void)videoDidFail:(TJPlacement*)placement error:(NSString*)errorMsg {
MPLogAdEvent([MPLogEvent adShowFailedForAdapter:NSStringFromClass(self.class) error:[NSError errorWithCode:MOPUBErrorUnknown localizedDescription:errorMsg]], self.placementName);
}
- (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