/
RNAdMobRewarded.m
159 lines (134 loc) · 4.4 KB
/
RNAdMobRewarded.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
#import "RNAdMobRewarded.h"
#if __has_include(<React/RCTUtils.h>)
#import <React/RCTUtils.h>
#else
#import "RCTUtils.h"
#endif
static NSString *const kEventAdLoaded = @"rewardedVideoAdLoaded";
static NSString *const kEventAdFailedToLoad = @"rewardedVideoAdFailedToLoad";
static NSString *const kEventAdOpened = @"rewardedVideoAdOpened";
static NSString *const kEventAdClosed = @"rewardedVideoAdClosed";
static NSString *const kEventAdLeftApplication = @"rewardedVideoAdLeftApplication";
static NSString *const kEventRewarded = @"rewardedVideoAdRewarded";
static NSString *const kEventVideoStarted = @"rewardedVideoAdVideoStarted";
@implementation RNAdMobRewarded
{
NSString *_adUnitID;
NSArray *_testDevices;
RCTPromiseResolveBlock _requestAdResolve;
RCTPromiseRejectBlock _requestAdReject;
BOOL hasListeners;
}
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
RCT_EXPORT_MODULE();
- (NSArray<NSString *> *)supportedEvents
{
return @[
kEventRewarded,
kEventAdLoaded,
kEventAdFailedToLoad,
kEventAdOpened,
kEventVideoStarted,
kEventAdClosed,
kEventAdLeftApplication ];
}
#pragma mark exported methods
RCT_EXPORT_METHOD(setAdUnitID:(NSString *)adUnitID)
{
_adUnitID = adUnitID;
}
RCT_EXPORT_METHOD(setTestDevices:(NSArray *)testDevices)
{
_testDevices = testDevices;
}
RCT_EXPORT_METHOD(requestAd:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
_requestAdResolve = resolve;
_requestAdReject = reject;
[GADRewardBasedVideoAd sharedInstance].delegate = self;
GADRequest *request = [GADRequest request];
request.testDevices = _testDevices;
[[GADRewardBasedVideoAd sharedInstance] loadRequest:request
withAdUnitID:_adUnitID];
}
RCT_EXPORT_METHOD(showAd:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
if ([[GADRewardBasedVideoAd sharedInstance] isReady]) {
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIViewController *rootViewController = [keyWindow rootViewController];
[[GADRewardBasedVideoAd sharedInstance] presentFromRootViewController:rootViewController];
resolve(nil);
}
else {
reject(@"E_AD_NOT_READY", @"Ad is not ready.", nil);
}
}
RCT_EXPORT_METHOD(isReady:(RCTResponseSenderBlock)callback)
{
callback(@[[NSNumber numberWithBool:[[GADRewardBasedVideoAd sharedInstance] isReady]]]);
}
- (NSDictionary<NSString *,id> *)constantsToExport
{
return @{
@"simulatorId": kGADSimulatorID
};
}
- (void)startObserving
{
hasListeners = YES;
}
- (void)stopObserving
{
hasListeners = NO;
}
#pragma mark GADRewardBasedVideoAdDelegate
- (void)rewardBasedVideoAd:(__unused GADRewardBasedVideoAd *)rewardBasedVideoAd
didRewardUserWithReward:(GADAdReward *)reward {
if (hasListeners) {
[self sendEventWithName:kEventRewarded body:@{@"type": reward.type, @"amount": reward.amount}];
}
}
- (void)rewardBasedVideoAdDidReceiveAd:(__unused GADRewardBasedVideoAd *)rewardBasedVideoAd
{
if (hasListeners) {
[self sendEventWithName:kEventAdLoaded body:nil];
}
_requestAdResolve(nil);
}
- (void)rewardBasedVideoAdDidOpen:(__unused GADRewardBasedVideoAd *)rewardBasedVideoAd
{
if (hasListeners) {
[self sendEventWithName:kEventAdOpened body:nil];
}
}
- (void)rewardBasedVideoAdDidStartPlaying:(__unused GADRewardBasedVideoAd *)rewardBasedVideoAd
{
if (hasListeners) {
[self sendEventWithName:kEventVideoStarted body:nil];
}
}
- (void)rewardBasedVideoAdDidClose:(__unused GADRewardBasedVideoAd *)rewardBasedVideoAd
{
if (hasListeners) {
[self sendEventWithName:kEventAdClosed body:nil];
}
}
- (void)rewardBasedVideoAdWillLeaveApplication:(__unused GADRewardBasedVideoAd *)rewardBasedVideoAd
{
if (hasListeners) {
[self sendEventWithName:kEventAdLeftApplication body:nil];
}
}
- (void)rewardBasedVideoAd:(__unused GADRewardBasedVideoAd *)rewardBasedVideoAd
didFailToLoadWithError:(NSError *)error
{
if (hasListeners) {
NSDictionary *jsError = RCTJSErrorFromCodeMessageAndNSError(@"E_AD_FAILED_TO_LOAD", error.localizedDescription, error);
[self sendEventWithName:kEventAdFailedToLoad body:jsError];
}
_requestAdReject(@"E_AD_FAILED_TO_LOAD", error.localizedDescription, error);
}
@end